Is it possible to do a grep count of multiple occurrences in a file in one single command? For example:
$ cat > file
blah alfa
beta blah
blah blahgamma
gamma
I can do:
grep -c 'alfa' file
1
grep -c 'beta' file
1
grep -c 'gamma' file
2
But is it possible to so domething like:
grep -c -e 'alfa' -e 'beta' -e 'gamma' -somemoreblackmagic file
and get the counts for each of them?
alfa 1
beta 1
gamma 2
Best Answer
I don't think grep is capable of what you want to do.
Just use awk instead:-)
This solution may not work well for large files (is not optimized). And works for plain words only - not regexps. But it's easy to add some features if so desired.
Low end version with restrictions outlined in comments below:
just give the search strings directly to the script
[EDIT]
fixed version with regex support (see comment below). Please tell me if there still are any open issues.
sample usage:
gives:
regex usage:
gives:
[/EDIT]