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    
 
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: 18 August 2016, 1:55 AM EDT
Posts: 766
Thanks: 564
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 Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 25 August 2016, 4:20 PM EDT
Location: Aachen, Germany
Posts: 9,231
Thanks: 181
Thanked 2,686 Times in 2,516 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: 25 August 2016, 6:13 PM EDT
Location: Foxborough, MA
Posts: 7,961
Thanks: 186
Thanked 689 Times in 645 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: 11 August 2016, 5:28 AM EDT
Location: Mumbai, India
Posts: 1,688
Thanks: 138
Thanked 204 Times in 198 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: 28 July 2016, 6:37 AM EDT
Location: India
Posts: 1,413
Thanks: 101
Thanked 497 Times in 474 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: 17 August 2016, 12:59 PM EDT
Location: 44.21.48N 80.50.15W
Posts: 1,864
Thanks: 1
Thanked 233 Times in 220 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: 13 July 2016, 12:15 AM EDT
Posts: 1,950
Thanks: 11
Thanked 300 Times in 271 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 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 11:20 PM.