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

3
  • 1
    Have you seen awk's NR variable? Commented Feb 21, 2017 at 3:24
  • unix.stackexchange.com/a/322171/135943 Commented Feb 21, 2017 at 3:48
  • 1
    You can't say "I'm moving to awk". That would be like saying "I'm starting to see the benefits of a hammer over a screwdriver. I think I'll be using the hammer from now on." It doesn't make sense. Commented Feb 21, 2017 at 7:57

2 Answers 2

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.

2

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

1
  • You misread the question; should be awk 'NR <= 5'. And you don't need to call print explicitly. Commented Feb 21, 2017 at 3:48

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.