|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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: 1Or, for one single line, try Code:
$ awk '{print gsub (srch,srch)}' srch="money king" file
2Last 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 | ||
|
|
#4
|
|||
|
|||
|
Or , Code:
awk '{s+=gsub(/money king/,"money king") END {print s}}' filewith 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
|
||||
|
||||
|
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.. |
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
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 $countRun 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
|
||||
|
||||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|