1

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 1

1
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
10
  • Do I need to type this command on the command line? for f in ???.txt do awk program.awk "$f" > "output$f" done
    – amatek
    Apr 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
  • Thanks a lot. I'll give you feedback when I get to my PC.
    – amatek
    Apr 25, 2015 at 3:09
  • It worked. How about processing all the .txt files in the folder, despite the number of characters in the file names?
    – amatek
    Apr 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!
    – Janis
    Apr 25, 2015 at 13:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .