Count how many times each word from a word list appears in a file?
bash, grep
Solution
You can do this in a loop that reads a single word at a time from a word-list file, and then counts the instances in a data file. For example:
while read; do
echo -n "$REPLY "
fgrep -ow "$REPLY" data.txt | wc -l
done < <(sort -u word_list.txt)
The "secret sauce" consists of:
- using the implicit REPLY variable;
- using process substitution to collect words from the word-list file; and
- ensuring that you are grepping for whole words in the data file.
Problem
I have a file, `list.txt` which contains a list of words. I want to check how many times each word appears in another file, `file1.txt`, then output the results. A simple output of all of the numbers sufficient, as I can manually add them to `list.txt` with a spreadsheet program, but if the script adds the numbers at the end of each line in `list.txt`, that is even better, e.g.: ``` bear 3 fish 15 ``` I have tried this, but it does not work: ``` cat list.txt | grep -c file1.txt ```