Bash to find count of multiple strings in a large file Bash to find count of multiple strings in a large file unix unix

Bash to find count of multiple strings in a large file


grep -Eo 'pig|horse|cat' txt.file | sort | uniq -c | awk '{print $2": "$1}'

Breaking that into pieces:

grep -Eo 'pig|horse|cat'  Print all the occurrences (-o) of the                          extended (-e) regex sort                      Sort the resulting wordsuniq -c                   Output unique values (of sorted input)                          with the count (-c) of each valueawk '{print $2": "$1}'    For each line, print the second field (the word)                          then a colon and a space, and then the first                          field (the count).