Search code examples
linuxshellgrepwc

Using grep, to search the files in the directories, how can I get the occurrence count of each word using a list.txt(or csv)?


In the list.txt I have:

Lucas
Viny
Froid

In current directory, I have a lot csv files containing names.

I need to know how many times each word of my list appears on these csv files.

I tried:

grep -riohf list.txt . | wc -lw

But it return only the counts. I need to know which word each count refers to.

I just need something like that:

Lucas 353453
Viny 9234
Froid 934586

Solution

  • Using grep and wc within a loop, you can count each individual occurrence of a word rather than just the lines.

    while read -r line; do
        count=$(grep -o "$line" *.csv | wc -l)
        echo "$line $count"
    done < list.txt