awkコードを変更する場合、単一のawkプロセスで解決でき、シェルループはありません。
awk 'FNR==1{if(o)close(o);o=FILENAME;sub(/\.tex/,"_sorted.tex",o)}{ORS=FNR%3?" ":"\n";print>o}' *.tex
美しさではなく、わずかに速くなります。
コメントで要求された説明。
FNR(f ile n umberまたはr ecord)はNR(n umberまたはr ecord)に似ていNRますが、FNRはすべての入力レコードの連続シーケンス番号ですが、新しい入力ファイルの処理が開始されると1にリセットされます。
のgawk4.0のみの代替FNR==1はBEGINFILE特別なパターンです。
awk '
FNR==1{ # first record of an input file?
if(o)close(o); # was previous output file? close it
o=FILENAME;sub(/\.tex/,"_sorted.tex",o) # new output file name
}
{
ORS=FNR%3?" ":"\n"; # set ORS based on FNR (not NR as in the original code)
print>o # print to the current output file
}
' *.tex
FNR==1です。=)