Sed print even/odd numbered lines

With sed you can print text from even or odd numbered lines. I will show with an example.

I have text file with name input.txt with content as shown below.

1

2

3

4

5

6

7

8

9

To print odd numbered lines from input.txt:

$ sed -n "1~2 p" input.txt

Output for above command:

1

3

5

7

9

To print even numbered lines from input.txt:

$ sed -n "2~2 p" input.txt

Output for above command:

2

4

6

8

To print text from a specific line number:

$ sed -n "2 p" input.txt

Output:(it will print text at line number 2)

2

-Sany

Advertisements

3 comments on “Sed print even/odd numbered lines

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s