39

I'm trying to delete the first two lines of a file by just not printing it to another file. I'm not looking for something fancy. Here's my (failed) attempt at awk:

awk '{ (NR > 2) {print} }' myfile

That throws out the following error:

awk: { NR > 2 {print} }
awk:          ^ syntax error

Example:

contents of 'myfile':

blah
blahsdfsj
1 
2
3
4

What I want the result to be:

1
2
3
4
Sam
  • 7,252
  • 16
  • 46
  • 65
Amit
  • 7,688
  • 17
  • 53
  • 68

4 Answers4

77

Use tail:

tail -n+3 file

from the man page:

   -n, --lines=K
          output the last K lines, instead of the last 10; or use  -n  +K
          to output lines starting with the Kth
RobS
  • 3,807
  • 27
  • 34
31

How about:

tail +3 file

OR

awk 'NR>2' file

OR

sed '1,2d' file
anubhava
  • 761,203
  • 64
  • 569
  • 643
26

You're nearly there. Try this instead:

awk 'NR > 2 { print }' myfile

awk is rule based, and the rule appears bare (i.e., without braces) before the block it woud execute if it passes.

Also as Jaypal has pointed out, in awk if all you want to do is print the line that matches the rules you can even omit the action, thus simplifying the command to:

awk 'NR > 2' myfile
Chris J
  • 30,688
  • 6
  • 69
  • 111
  • He could also put an `if` before parentheses, though your way is better, since it doesn't engage every line, just the ones that already match. – Dan Fego Jan 13 '12 at 21:55
  • 1
    You dont even need `{ print }`. `awkish` way would be `awk 'NR>2' myfile` – jaypal singh Jan 13 '12 at 22:39
6

awk is based on pattern{action} statements. In your case, the pattern is NR>2 and the action you want to perform is print. This action is also the default action of awk.

So even though

awk 'NR>2{print}' filename

would work fine, you can shorten it to

awk 'NR>2' filename.

jaypal singh
  • 74,723
  • 23
  • 102
  • 147