Search code examples
bashgrepfind

How to display all the files in a directory which contains a word a certain number of times?


I currently use this command find -name "*.xml" | xargs grep -c -H -w "word" to display all the XML files in a directory which contains a specific word and how many times. It works quite well (because the word I search appears only once per line, if it wasn't the case I should find another solution because of grep) :

./file1.xml:2
./file2.xml:3
./file3.xml:6

but what is really interesting for me is to only display words with the more word matching.

Anyone knows how to filter this output with only the files with the highest scores ?

thx!


Solution

  • Pipe to awk to compare the counts with the minimum.

    find -name "*.xml" | xargs grep -c -H -w "word" | awk -F: '$NF > 3'