3

I am trying to apply one awk script to all the files in one folder, I am using:

for %%i in (*.txt) do (awk -f NormalizeFiles.awk "%%i" > new\"%%i")

but then I get the message:

Syntactic error near the unexpected element '('

I don't know what is the problem.

3
  • 1
    Are you using bash? That attempted loop syntax doesn't look very similar to bash, maybe csh?
    – jordanm
    Apr 23, 2016 at 2:42
  • You are right @jordanm it's not bash. Thanks for noticing Apr 23, 2016 at 4:40
  • 1
    It's the syntax for Windows CMD when using a 'batch' file (interactive the % signs are single not double). Backslash as a pathname delimiter is also Windows. Apr 23, 2016 at 9:00

2 Answers 2

7

It appears you are trying to run input files through awk and save the results as a different file name. Unless you need very special file naming, this should serve:

for i in *.txt; do
    awk -f NormalizeFiles.awk $i > $i.out
done

Although your question is tagged bash, your syntax, as someone pointed out, is not bash, e.g. the misuse of parentheses. The %% syntax is not used correctly, and it doesn't seem germane to your goal; however, you should check out http://www.tldp.org/LDP/abs/abs-guide.pdf if you want to know more about it.

1
  • That works just fine!! Thanks a lot! I found that line of command when I was trying to solve the problem but I think you are right, it's not bash. And thanks for the link! Apr 23, 2016 at 4:37
6

awk can write to different files by itself.

e.g.

awk '{print > "new/"FILENAME}' *.txt

You could use something like that inside your NormalizeFiles.awk script.

If you don't want to type > "new/"FILENAME for every print statement, you could do something like this:

awk 'FNR == 1 { out = "new/"FILENAME } ; { print > out }' *.txt

That changes the output file (in variable out) every time the script sees the first line (FNR == 1) of a new file.

BTW, if your script has #!/usr/bin/awk -f as the first line and is made executable with chmod, you can run it directly as just ./NormalizeFiles.awk - same as you would for an executable #!/bin/bash or #!/usr/bin/perl etc script.

2
  • you are right @cas it's not bash. And I am going to do use that in the awk script. Thanks a lot! Apr 23, 2016 at 4:43
  • @LeonardoAyala if any of the answers given have solved your problem, select the one you like best and click on the big tick to the left to mark it as the accepted answer.
    – cas
    Apr 23, 2016 at 5:46

You must log in to answer this question.

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