Skip the first 6 lines/rows in a text file with awk

0

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:

BEGIN {
} 

{ 
process here
} 

END {

}

My text file is like this:

0
3
5
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
.
.
.

amatek

Posted 2015-04-23T04:01:52.807

Reputation: 135

Answers

1

Use:

BEGIN {
} 

NR>6{ 
process here
} 

END {

}

In awk, NR is the line number. To skip processing for the first six, we add the condition NF>6. When this condition is false, which it is for the first six lines, the lines are not processed.

Processing multiple files

Suppose that we have a folder full of text files named 000.txt to 181.txt. Here is an example of processing all of them with the same awk script and send them to respective output files (output000.txt - output181.txt):

awk 'NR>6{print>("output" FILENAME)}' {000..181}.txt

If we are using bash, then {000..181}.txt will expand to the names of our 182 input files.

In awk (this may require GNU awk), FILENAME is the name of the input file that awk is currently working on. Thus ("output" FILENAME) is the name of our current output file.

Intended just as an example, the above simply prints all but the first six lines of the input file to the output file. More complicated programs would use the same principles.

Extra: To process every file in the directory, regardless of its file name, use:

awk 'NR>6{print>("output" FILENAME)}' *

John1024

Posted 2015-04-23T04:01:52.807

Reputation: 13 893

I have a folder full of text files named 000.txt to 181.txt. How can I process all of them (on the command line) with the same awk script (program.awk) and send them to their respective output files (output000.txt - output181.txt)? – amatek – 2015-04-25T02:59:29.497

@amatek See the updated answer for an example of processing input files 000.txt to 181.txt into output files output000.txt - output181.txt. – John1024 – 2015-04-25T03:37:46.960

Thanks a lot. How about processing 'every' file in the folder despite the characters in the name? – amatek – 2015-04-25T11:51:13.963

@amatek See the end of the updated answer for processing 'every' file. – John1024 – 2015-04-25T18:56:29.637