Issue
How do I use grep to count the number of occurrences of a string?
input:
.
├── a.txt
├── b.txt
// a.txt
aaa
// b.txt
aaa
bbb
ccc
Now I want to know how many times aaa
and bbb
appear.
output:
aaa: 2
bbb: 1
Solution
Just an idea:
grep -E "aaa|bbb|ccc" *.txt | awk -F: '{print $2}' | sort | uniq -c
This means:
grep -E "...|..." : extended grep, look for all entries
The result is given as:
a.txt:aaa
b.txt:aaa
b.txt:bbb
b.txt:ccc
awk -F: '{print $2}' : split the result in 2 columns,
based on the semicolon,
and only show the second column
sort | uniq -c : sort and count unique entries
Answered By - Dominique