Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing two files and reporting a count of matching rows

Status
Not open for further replies.

2ITLTD

Technical User
Sep 19, 2002
17
GB
There are 2 files...1 is a log file, the other is a list of names...i need a script that will count the number of times the names in the second file appears in the log file
hope that makes sense?
e.g.
i have a file where i list all the names and the second file is a novel that has names in it. I need to write a script to count how many times each name appears in the novel.
 
Hi

Hmm... Or maybe...
Code:
while read s; do echo "$s : $( grep -c "$s" /path/to/log_file )"; done < /path/to/second_file
If not, try to rephrase your question.

Feherke.
 
Thank you very much for your prompt reply, i had to modify it a little as it did not work 100% probably down to Unix OS or shell used.

/tmp/file1 :
Mathew
Mark
Luke
John

/tmp/file2 :
Once upon a time there was a man called Mathew,
mathew was a very nice person.
He was very bored and wanted to play with his friends (Mark, and Luke) together with lukes
friend John.

Output from running unix script is:-

Mathew : 2
Mark : 1
Luke : 2
John : 1

Unix shell script is:

while read s
do
echo "$s : " `egrep -i -c "$s" /tmp/file2`
done < /tmp/file1

The only other problem i has was that the line with ...Mathew, mathew... would not get picked up twice unless they were split onto seperate lines! as per my example.

Thanks again.
 
Hi

This will slow it down abit, but will find both Lukes.
Code:
while read s; do echo "$s : " `tr ' ' '\n' < /tmp/file2 | egrep -i -c "$s"`; done < /tmp/file1
Or if your words are not always delimited with space ( the author did not put space after punctuation marks ) :
Code:
while read s; do echo "$s : " `tr -s '[:punct:]' '\n' < file2 | egrep -i -c "$s"`; done < file1

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top