Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


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

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-20-2013
Registered User
 
Join Date: Dec 2006
Posts: 500
Thanks: 265
Thanked 1 Time in 1 Post
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
Location: Aachen, Germany
Posts: 2,030
Thanks: 25
Thanked 460 Times in 445 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
ɹoʇɐɹǝpoɯ
 
Join Date: Feb 2005
Location: Foxborough, MA
Posts: 7,384
Thanks: 112
Thanked 486 Times in 458 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
Location: Mumbai,India
Posts: 1,420
Thanks: 101
Thanked 133 Times in 129 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
Gotham Knight
 
Join Date: Mar 2012
Location: India
Posts: 1,396
Thanks: 91
Thanked 490 Times in 468 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
Location: 44.21.48N 80.50.15W
Posts: 1,425
Thanks: 1
Thanked 131 Times in 126 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
Registered User
 
Join Date: Apr 2009
Posts: 1,774
Thanks: 6
Thanked 239 Times in 216 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
Reply

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 02:29 PM.