Search code examples
linuxbashunixawkfile-globs

Applying AWK on Set of Files with Same Extension


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.


Solution

  • 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.