28

HI,

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

Thanks

42

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

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

| improve this answer | |
  • 1
    {print} is the default action, so awk 'NR % 2 == 0' < myfile is enough. – Roberto Bonvallet Jan 28 '10 at 13:47
  • 3
    @roberto, don't need the input redirection. – user31894 Jan 28 '10 at 16:12
  • 1
    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
  • Worked perfectly with pipe cat filename | awk 'NR % 2 == 1' (for odd lines). A third-party program was adding an unwanted newline after each line, so I piped its output directly into awk for cleanup. – hsandt Jul 24 at 22:06
13

Or something like

sed -n 'p;n' filename

For explanation look-up the man page for sed.

| improve this answer | |
  • This is the portable sed version that slipped my mind. – Chris Johnsen Jan 28 '10 at 13:27
  • 1
    you can easily skip lines upfront with sed -n '1d;p;n' for the even lines for example – kisp Mar 6 '17 at 16:09
7

Use sed:

sed -n 1~2p filename
| improve this answer | |
  • 1
    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 gnu.org/software/sed/manual/html_node/Addresses.html#Addresses 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
2
awk '{if(NR%2!=0)print}' myfile
| improve this answer | |
2

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"
| improve this answer | |
1

A simple search in Google shows this link: http://bash.cyberciti.biz/academic/write-odd-even-line-file/

You only need to tweak it a little bit.

My 2 cents.

| improve this answer | |
0

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.

| 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
-1

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

| 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
-2

You can use these commands for require result

awk 'NR%2 == 1'

sed -ne '1~2p'

| improve this answer | |
  • 2
    -1. This duplicates answers from almost 10 years ago, minor differences are not explained at all. And why is ls -l here? – Kamil Maciorowski Jul 3 '19 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy