18

I am processing a file with awk and need to skip some lines. The internet dosen't have a good answer.

So far the only info I have is that you can skip a range by doing:

awk 'NR==6,NR==13 {print}' input.file

OR

awk 'NR <= 5 { next } NR > 13 {exit} { print}' input.file

You can skip the first line by inputting:

awk 'NR < 1 { exit } { print}' db_berths.txt

How do you skip the last line?

ovatsug25
  • 7,786
  • 7
  • 34
  • 48

2 Answers2

20

One way using awk:

awk 'NR > 1 { print prev } { prev = $0 }' file.txt

Or better with sed:

sed '$d' file.txt
Steve
  • 51,466
  • 13
  • 89
  • 103
  • 3
    The exact format of `head`'s options depends on your operating system. rwos' suggestion works in Linux, but not in FreeBSD, OpenBSD, OS/X. Probably also doesn't work in Solaris, HP/UX, etc. – ghoti Aug 09 '12 at 11:36
  • 1
    [For those who didn't follow the awk version](http://stackoverflow.com/a/15856794/645270) – keyser Feb 14 '15 at 00:43
-4

You can try:

awk 'END{print NR}' file
sloth
  • 99,095
  • 21
  • 171
  • 219
giddi
  • 103
  • 5
  • 1
    -1 this will print how many lines in the input file. completely different from OP's requirement – Kent Aug 08 '12 at 15:31