HowTo: Remove Every Other Line in Text Files – Linux

Let’s say you’ve got a text file, of any size, big or small, and you want to remove every other line of that file, well here are a few commands in Linux that allow you to do this.

Example, you want to get from this:

1
2
3
4
5
6
7
8
9
10

To this:

1
3
5
7
9

The sed way:

 sed -n "p;N;" file.txt > newfile.txt

The awk way:

 awk 'NR%2 != 0' file.txt > newfile.txt

Here you can actually specify N lines, replace 2 in the above command and you’ll be able to take out every N’th number. As an example, here’s the above replaced with a 3 on the file:

1
2
4
5
7
8
10

Easy as pie, right?

1 comment to HowTo: Remove Every Other Line in Text Files – Linux

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>