I am new in awk and I do not know if it is possible to write an awk script that does this:
I have hundred of data files that I have to sort. For each one I use the following one-liner:
awk 'ORS=NR%3?" ":"\n" ' file1.tex > file1_sorted.tex
awk 'ORS=NR%3?" ":"\n" ' file2.tex > file2_sorted.tex
...
and I get the output I need. However I would like to have an script to automate this action, taking each file, applying the action and writing the corresponding sorted file.
I would appreciate your help!
Best Answer
If you modify the
awk
code, can be solved by a singleawk
process and no shell loop:Not a beauty, just insignificantly faster.
Explanations as requested in comment.
FNR
(file number or record) is similar toNR
(number or record), but whileNR
is a continuous sequence number of all input records,FNR
is reset to 1 when processing of a new input file is started.A
gawk
4.0 only alternative for theFNR==1
is theBEGINFILE
special pattern.