Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Count number of occurrence of a string in file

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-20-2013
SkySmart SkySmart is offline
Registered User
 
Join Date: Dec 2006
Last Activity: 11 May 2015, 5:08 PM EDT
Posts: 677
Thanks: 445
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 Unix and Linux 01-20-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 22 May 2015, 5:26 AM EDT
Location: Aachen, Germany
Posts: 5,945
Thanks: 105
Thanked 1,588 Times in 1,492 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 Unix and Linux 01-20-2013
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 21 May 2015, 5:12 PM EDT
Location: Foxborough, MA
Posts: 7,822
Thanks: 174
Thanked 652 Times in 610 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 Unix and Linux 01-20-2013
clx clx is offline Forum Advisor  
Registered User
 
Join Date: Jun 2007
Last Activity: 22 May 2015, 4:52 AM EDT
Location: Mumbai, India
Posts: 1,677
Thanks: 132
Thanked 200 Times in 194 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 Unix and Linux 01-20-2013
elixir_sinari's Unix or Linux Image
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
 
Join Date: Mar 2012
Last Activity: 16 March 2015, 7:53 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 Unix and Linux 01-20-2013
jgt's Unix or Linux Image
jgt jgt is offline Forum Advisor  
Registered User
 
Join Date: Apr 2007
Last Activity: 20 May 2015, 8:13 AM EDT
Location: 44.21.48N 80.50.15W
Posts: 1,616
Thanks: 1
Thanked 166 Times in 158 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 Unix and Linux 01-20-2013
durden_tyler's Unix or Linux Image
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
 
Join Date: Apr 2009
Last Activity: 5 April 2015, 4:04 PM EDT
Posts: 1,904
Thanks: 9
Thanked 285 Times in 259 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 04:38 PM.