How can I skip the first 6 lines/rows in a text file (input.txt) and process the rest with awk? The format of my awk script (program.awk) is:


process here



My text file is like this:

0.1 4.3
2.0 1.5
1.5 3.0
0.3 3.3
1.5 2.1

I want to process the file starting from:

0.3 3.3
1.5 2.1

Use either of the two patterns:

NR>6 { this_code_is_active }

or this:

NR<=6 { next }
{ this_code_is_active }

Use FNR instead of NR if you have many files as arguments to awk and want to skip 6 lines in every file.



awk 'FNR > 6 { #process here }' file
  • 4
    Good! But you didn't explain why this is better - for multiple files FNR is the row number in each file, while NR is the number in the whole input (not an issue when piping). – Tomasz Gandor Aug 8 '17 at 5:46

You may also skip an arbitrary number of lines at the beginning or the end of the file using head or tail programs.

For your concrete question,

tail input.txt -n+7 | program.awk

will do, provided your program.awk file is executable. Otherwise, you may use

tail input.txt -n+7 | awk -f program.awk

This way, you will spare a comparison for each line and you don't need to change the logic of your AWK code.

tail will start streaming text starting at the seventh line, skipping the six first lines.

This will not be a huge deal in performance, especially if text process is simple thanks to caching. However, for long files and repeated use in cloud environment may save some cost.

  • Correct, but as a rule of thumb, one should avoid piping when you can easily do it with one tool alone. Think of a huge text file piping through both commands just to remove a few lines. – Philippos Sep 1 '17 at 11:21

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.