Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


probably is this something very easy, but I do not get the point now. In Linux, shell, given a text file, how can I do something like, cat filename so I only see the first line, the third line, 5, 7, 9 and so on?

if not with cat, with another command


share|improve this question

migrated from Jan 28 '10 at 16:58

This question came from our site for professional and enthusiast programmers.

Using awk: awk 'NR % 2 == 0' filename

Edit: removed {print}, as shown in ghostdog74's answer and commented on by Roberto Bonvallet.

share|improve this answer
{print} is the default action, so awk 'NR % 2 == 0' < myfile is enough. – Roberto Bonvallet Jan 28 '10 at 13:47
@roberto, don't need the input redirection. – user31894 Jan 28 '10 at 16:12
Just as a note, this shows even lines (where the first line is 1) and odd lines (if you think of the first line as 0). Replace 2 == 0 with 2 == 1 for the opposite. – cde Sep 14 '13 at 20:27

Or something like

sed -n 'p;n' filename

For explanation look-up the man page for sed.

share|improve this answer
This is the portable sed version that slipped my mind. – Chris Johnsen Jan 28 '10 at 13:27

Use sed:

sed -n 1~2p filename
share|improve this answer
This is a GNU extension. It may not work systems where GNU sed is not the default. – Chris Johnsen Jan 28 '10 at 13:24
True but should work with most flavours of Linux – Dolphenstein Jan 28 '10 at 13:26
Hmm.. I just tried "sed --posix -n 1~2p filename" and that seems to work as well. The --posix is supposed to remove support for gnu extensions. – Dolphenstein Jan 28 '10 at 13:30
The GNU sed manual says “first~step This GNU extension…”. It does not mention --posix. When I try gsed --posix -n 1~2p (GNU sed 4.2.1), I get gsed: -e expression #1, char 2: unknown command: ‘~’. – Chris Johnsen Jan 28 '10 at 13:37
Ok.. makes sense. Dont know whats wrong with my sed. I'm using cygwin, so that might have something to do with it. – Dolphenstein Jan 28 '10 at 13:43
awk '{if(NR%2!=0)print}' myfile
share|improve this answer

2 ways you can do it with awk

$ awk '{getline;print}' file # the equivalent of the selected sed answer
$ awk 'NR%2==0' file

OR with just the shell

i=1; while read -r line; do [ $((i++ % 2)) -eq 0 ] && echo $line; done <"file"
share|improve this answer

A simple search in Google shows this link:

You only need to tweak it a little bit.

My 2 cents.

share|improve this answer

Unable to test, found a link: cat -n $1 | grep "$ODDRE" | cut -b 8- > oddfile.$$

share|improve this answer
You missed out the most important thing... what ODDRE is set to. But that answer will only work if the file is less than a million lines. – Andrew McGregor Jan 28 '10 at 13:23

Haven't tested this (doesn't seem to work on Mac OS X), but GNU sed supports first~step as an address selection, so for example:

sed -n 1~2p file

would start at the first line and print every other line after that.

share|improve this answer
Right, this is a GNU extension. Mac OS X uses a BSD sed in the base installation. – Chris Johnsen Jan 28 '10 at 13:25

You must log in to answer this question.