3

This question already has an answer here:

I have a text file on a Linux system, and would like to remove every second line, which is "even" numbered. Example: I would like to remove the second line of the file, the fourth, sixth, eighth line, and so on, until you complete filtering of the file completely.

file.txt

86850343
88065952
89381968
89536251
89714939
89826424
90124775
90672109
91408453
92438737

I would like this output:

86850343
89381968
89714939
90124775
91408453

marked as duplicate by don_crissti, Sparhawk, terdon awk Jan 2 at 2:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3

With GNU Sed's n-skip-m notation:

$ sed '2~2d' file.txt
86850343
89381968
89714939
90124775
91408453
2

You can't really beat sed for this sort of thing, but here are some other options:

$ awk 'NR%2!=0' file
86850343
89381968
89714939
90124775
91408453

$ perl -ne '$.%2 && print' file
86850343
89381968
89714939
90124775
91408453

$ perl -ne 'print if $.%2' file
86850343
89381968
89714939
90124775
91408453

$ perl -ne 'not $.%2 && print' file
86850343
89381968
89714939
90124775
91408453
  • 2
    Heh, the first 3 of those give incorrect output based on the question; the fourth gives the right result :-) – Stephen Harris Jan 2 at 1:30
  • 1
    @StephenHarris eeeek! Completely fumbled that one. Thanks for the heads up, fixed now. – terdon Jan 2 at 1:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.