Applying AWK on Set of Files with Same Extension

1.3k views Asked by At

I want to apply the following "awk" command on files with extension "*.txt"

awk '$4 ~ /NM/{ sum += $2 } END{ print sum }'

But why this command doesn't work:

for i in *.txt do echo awk '$4 ~ /NM/{ sum += $2 } END{ print sum }' $i; done

Normally,

awk '$4 ~ /NM/{ sum += $2 } END{ print sum }' file1.txt

would work.

6

There are 6 answers

1
Douglas Leeder On BEST ANSWER

Once you've removed the echo it should work:

for i in *.txt do awk '$4 ~ /NM/{ sum += $2 } END{ print sum }' $i; done

It'll fail if there are any text files with spaces in them, you could try this:

find . -name '*.txt' -print0 | xargs --null -n 1 awk '$4 ~ /NM/{ sum += $2 } END{ print sum }'

An alternative for printing out names:

find . -name '*.txt' -print -exec awk '$4 ~ /NM/{ sum += $2 } END{ print sum }' {} \;

(Basically make find execute awk directly, so and also print out the file names.

0
Andy On

Not sure if you've copy pasted or it's a typo.

for i in *.txt do echo awk '$4 ~ /NM/{ sum += $2 } END{ print sum }' $i; done

With echo corrected, the command above will echo your awk script and the filename, but not run it.

0
serioys sam On

echo is not required.

try

for i in *.txt; do; awk '$4 ~ /NM/{ sum += $2 } END{ print sum }' $i; done

or

for i in *.txt; do awk '$4 ~ /NM/{ sum += $2 } END{ print sum }' $i; done

should work

1
x-way On
for i in *.txt; do echo "$i"; awk '$4 ~ /NM/{ sum += $2 } END{ print sum }' "$i"; done

This will print the names of the processed files together with the output of the awk command.

0
Dimitre Radoulov On

Try this (use nawk or /usr/xpg4/bin/awk on Solaris):

awk 'END {
  printf "%s: %.2f\n", fn, sum
  }
FNR == 1 {
  if (fn) printf "%s: %.2f\n", fn, sum
  fn = FILENAME
  sum = 0
  }
$4 ~ /NM/ { 
  sum += $2 
  }' *.txt 
0
Daniel Fanjul On

You need to add a ';' :

for i in *.txt; do ...

instead of

for i in *.txt do ...