AWK – Handling Multiple Input and Output Files

awkbashperl

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)?

Best Answer

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.

Related Question