unixgrepxargs

TO find word count file by file


Is it possible to determine the number of times a particular word appears in multiple files - file by file - using grep

the "-c" option but this returns the number of matching lines the particular word appears in - and would nto work as a line may contain multiple instances of the word

For example :

File A.txt: some 12345 words and risk and risk

File B.txt: and 12345 then another risk

File C.txt: dummy words

I would run grep -l "12345" *.txt | xargs grep ?????? risk - Expected output A.txt:2 B.txt:1


Solution

  • Something like this would count the words

    awk -v w=risk '{for(i=1;i<=NF;i++)if($i==w)f[FILENAME]++} END {for(n in f)print n,f[n]}' A.txt B.txt
    A.txt 2
    B.txt 1