-2

How to delete everything in a file after 5th line using awk?

I can do it by sed, but these days I am moving from sed to awk.

peterh
  • 9,488
  • 16
  • 59
  • 88
NILA
  • 1

2 Answers2

2

awk 'NR <= 5' inputfile would do it (it prints whenever NR, the record number, is less than or equal to five.

jordanm
  • 41,988
  • 9
  • 116
  • 113
dhag
  • 15,440
  • 4
  • 54
  • 65
  • You misread the question; should be `awk 'NR <= 5'`. And you don't need to call `print` explicitly. – Wildcard Feb 21 '17 at 03:48
2
awk 'NR > 5{exit}1' yourfile

would quit "awk" as soon as the 6th line is reached. But prior to that, the default action would apply, thereby printing lines 1..5.

Rakesh Sharma
  • 770
  • 4
  • 4