LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-27-2008, 01:16 AM   #1
raghu123
Member
 
Registered: May 2008
Posts: 34

Rep: Reputation: 15
multiple pattern search and count the no. of occurances


Hi......

I have a file like this having names...file name: names.txt

raj
joe
sush
farhan
raj
ilem
adam
robert
adam
adam
joe

I have the patterns to be checked in a file: pattern.txt

gilli
ricky
saeed
raj
ilem
adam
how
smith
joe


So, i want to search each $i of pattern.txt in names.txt and count the no. of occurances of every $i

Plz help......
 
Old 11-27-2008, 01:39 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you can use grep with -f option. pls also make an effort to code something next time.
 
Old 06-03-2009, 05:17 AM   #3
scriptblues
LQ Newbie
 
Registered: Jun 2009
Posts: 13

Rep: Reputation: 0
for i in `cat pattern.txt`;do k='grep $i names.txt | wc -l`; echo "$i is found $k times in names.txt" >> output.txt; done
 
Old 06-03-2009, 05:22 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by scriptblues View Post
for i in `cat pattern.txt`;do k='grep $i names.txt | wc -l`; echo "$i is found $k times in names.txt" >> output.txt; done
this is an old thread. also, you can use simply use grep -c. no need for that extra for loop and wc...
 
Old 06-03-2009, 04:50 PM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
grep -f names.txt pattern.txt| sort| uniq -c
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
command to count occurances of a string MicahCarrick Linux - Software 1 06-02-2007 10:43 PM
AWK/SED Multiple pattern matching over multiple lines issue GigerMalmensteen Programming 15 12-03-2006 05:08 PM
Count occurances of a string in a text file MicahCarrick Linux - Software 1 06-24-2006 04:25 PM
Pattern count in a file --> scripting froglinux Programming 7 06-17-2006 01:55 PM
finding multiple occurances of a pattern sharad Linux - General 3 05-24-2006 03:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration