Count number of occurrence of a string in file | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Count number of occurrence of a string in file

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-20-2013
SkySmart SkySmart is online now
Registered User
 
Join Date: Dec 2006
Last Activity: 1 October 2014, 1:47 AM EDT
Posts: 626
Thanks: 382
Thanked 2 Times in 2 Posts
Count number of occurrence of a string in file

if there's a file containing:

Code:
money king money queen money cat money also money king

all those strings are on one line in the file.

how can i find out how many times "money king" shows up in the line?


Code:
egrep -c "money king"

wont work.
Sponsored Links
    #2  
Old 01-20-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 30 September 2014, 10:26 AM EDT
Location: Aachen, Germany
Posts: 4,292
Thanks: 73
Thanked 1,035 Times in 980 Posts
For this simple sample, try
Code:
$ tr " " "\n" <file | awk '/money/{getline; Ar[$1]++}END{for (Ix in Ar) print "money "Ix": "Ar[Ix]}' 
money king: 2
money queen: 1
money also: 1
money cat: 1

Or, for one single line, try
Code:
$ awk '{print gsub (srch,srch)}' srch="money king" file
2


Last edited by RudiC; 01-20-2013 at 09:25 AM..
The Following User Says Thank You to RudiC For This Useful Post:
SkySmart (01-20-2013)
Sponsored Links
    #3  
Old 01-20-2013
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 1 October 2014, 12:45 AM EDT
Location: Foxborough, MA
Posts: 7,657
Thanks: 150
Thanked 582 Times in 547 Posts

Code:
awk '{n+=gsub("money king", "&")}END{print n}' myFile

The Following 2 Users Say Thank You to vgersh99 For This Useful Post:
clx (01-20-2013), SkySmart (01-20-2013)
    #4  
Old 01-20-2013
clx clx is offline Forum Advisor  
Registered User
 
Join Date: Jun 2007
Last Activity: 27 September 2014, 1:33 AM EDT
Location: Mumbai, India
Posts: 1,614
Thanks: 125
Thanked 184 Times in 178 Posts
Or ,


Code:
awk '{s+=gsub(/money king/,"money king") END {print s}}' file

with GNU grep


Code:
grep -o "money king" file | wc -l


Last edited by clx; 01-20-2013 at 09:27 AM.. Reason: fixed awk END part, thanks to vgersh99
The Following User Says Thank You to clx For This Useful Post:
SkySmart (01-20-2013)
Sponsored Links
    #5  
Old 01-20-2013
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
 
Join Date: Mar 2012
Last Activity: 9 September 2014, 8:08 AM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 496 Times in 473 Posts

Code:
perl -nle '$c+=()=/money king/g;END{print $c}' file

@clx, slight mistake there:

awk '{s+=gsub(/money king/,"money king")} END {print s}' file

Last edited by elixir_sinari; 01-20-2013 at 12:05 PM..
The Following 2 Users Say Thank You to elixir_sinari For This Useful Post:
clx (01-21-2013), SkySmart (01-20-2013)
Sponsored Links
    #6  
Old 01-20-2013
jgt's Avatar
jgt jgt is offline Forum Advisor  
Registered User
 
Join Date: Apr 2007
Last Activity: 24 September 2014, 1:57 PM EDT
Location: 44.21.48N 80.50.15W
Posts: 1,553
Thanks: 1
Thanked 158 Times in 150 Posts

Code:
#!/bin/bash
#script t2
k=0                               
count=0                           
while read word                   
do                                
if [ "$word" = "$1" ]             
then                              
        k=1                       
else                              
if [ "$word" = "$2" -a $k -eq 1 ] 
then                              
        count=`expr $count + 1`   
        k=0                       
else                              
        k=0                       
fi                                
fi                                
done                              
echo $count

Run as

Code:
tr " " "\n" <mfile|./t2 money king

The Following User Says Thank You to jgt For This Useful Post:
SkySmart (01-20-2013)
Sponsored Links
    #7  
Old 01-20-2013
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
 
Join Date: Apr 2009
Last Activity: 27 September 2014, 12:05 PM EDT
Posts: 1,841
Thanks: 7
Thanked 266 Times in 241 Posts

Code:
$
$
$ cat f10
money king money queen money cat money also money king
$
$ perl -plne '$_=s/money king//g' f10
2
$
$

The Following User Says Thank You to durden_tyler For This Useful Post:
SkySmart (01-20-2013)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to count the number of occurrence of words from multiple files? Misa-Misa Shell Programming and Scripting 4 06-06-2012 02:40 AM
Count number of occurrence at each line fuad_ Shell Programming and Scripting 4 05-29-2012 05:25 AM
find string nth occurrence in file and print line number tmalik79 Shell Programming and Scripting 6 10-10-2011 02:20 AM
Replace x Number of String Occurrence with Sed SkySmart Shell Programming and Scripting 10 04-12-2011 02:56 PM
How to count number of occurances of string in a file? Shirisha Shell Programming and Scripting 7 10-13-2010 06:21 AM



All times are GMT -4. The time now is 01:58 AM.