I have a folder full of text files named 000.txt to 181.txt. How can I process all of them with the same awk script (program.awk) and send them to their respective output files (output000.txt - output181.txt)?
1 Answer
for f in ???.txt do awk -f program.awk "$f" > "output$f" done
will process all files whose names are three characters (any characters)
followed by .txt
. To restrict it to only files whose names
are three digits followed by .txt
, use
for f in [0-9][0-9][0-9].txt
-
Do I need to type this command on the command line? for f in ???.txt do awk program.awk "$f" > "output$f" done– amatekApr 25, 2015 at 3:01
-
You can type it as four separate lines, as I showed it. But, if you want to type it all on one line, you need to add semicolons, as in
for f in ???.txt; do awk program.awk "$f" > "output$f"; done
. Apr 25, 2015 at 3:07 -
-
It worked. How about processing all the .txt files in the folder, despite the number of characters in the file names?– amatekApr 25, 2015 at 11:45
-
@G-Man; I wonder why your code would work (as the OP says); the
awk
call is lacking the-f
switch!– JanisApr 25, 2015 at 13:34