Hi.
Most versions of
grep can handle a file of patterns, so that standard *nix utlities can be used:
Code:
#!/bin/bash -
# @(#) s3 Demonstrate string count total from files.
echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1) grep sort uniq
set -o nounset
echo
echo " strings file:"
cat strings
echo
echo " data files" data* ":"
cat -n data*
echo
echo " Results:"
grep -h -f strings data* |
sort |
uniq -c
exit 0
Producing:
Code:
% ./s3
(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0
grep (GNU grep) 2.5.1
sort (coreutils) 5.2.1
uniq (coreutils) 5.2.1
strings file:
dog
horse
cat
data files data1 data2 data3 data4 :
1 File 1
2 monkey
3 cat
4 dog
5 dog
6 File 2
7 horse
8 sawhorse
9 Files 3
10 cat
11 horse
12 witch
13 seven
14 File 4
15 spider
16 hoarse
17 horse
18 horse
19 horse
20 cat
Results:
3 cat
2 dog
5 horse
1 sawhorse
The files are filtered for the lines that contain strings of interest. Then, in order to count with
uniq, we need to sort the result.
If you need better filtering, you may need to change the patterns in the strings file, or -- in some versions of
grep -- use the "word" option "-w".
Adjust as necessary for your environment according to your man pages ... cheers, drl