how many times a word occur in afile | Unix Linux Forums | Shell Programming and Scripting

  Unix/Linux 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.

how many times a word occur in afile

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-03-2008
madhu.it madhu.it is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 11 October 2008, 5:10 AM EDT
Location: andhra pradesh
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
how many times a word occur in afile

i want a shell script program for how many times a word occur in a file.
i need not the line number but i want the counts of the particular word
for eg:-
hai how r u..
i am from andhra pradesh..
i am from tenali.i need this answer.i need it urgently..
i hope u will answer this ...
this is the matter in a file..i want to count the word "i" here..
my answer is i-11........
like this give a shell script program

Last edited by madhu.it; 10-03-2008 at 01:49 AM..
Sponsored Links
    #2  
Old Unix and Linux 10-03-2008
Yogesh Sawant's Unix or Linux Image
Yogesh Sawant Yogesh Sawant is offline Forum Advisor  
Full Time Dad
 
Join Date: Sep 2006
Last Activity: 30 December 2014, 12:28 PM EST
Location: Rossem, Tazenda
Posts: 1,211
Thanks: 6
Thanked 18 Times in 17 Posts
check this
Sponsored Links
    #3  
Old Unix and Linux 10-03-2008
nua7 nua7 is offline
Registered User
 
Join Date: Mar 2008
Last Activity: 17 February 2015, 8:05 AM EST
Location: /bin/sh
Posts: 429
Thanks: 26
Thanked 4 Times in 4 Posts
MySQL


Code:
grep -w "echo" tst | wc -w

echo is the word you are looking for and tst is the file.
    #4  
Old Unix and Linux 10-03-2008
madhu.it madhu.it is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 11 October 2008, 5:10 AM EDT
Location: andhra pradesh
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
it not gives perfect count.......
Sponsored Links
    #5  
Old Unix and Linux 10-03-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 10 Times in 8 Posts
grep just reports the number of lines which match (and you should use grep -c rather than pipe to wc).

If you have grep -o then you can use that:


Code:
grep -w -o echo tst | wc -l

In your example you seem to want to count the word-internal occurrences as well; take out the -w option in that case.

Please use code tags to make your examples clearer.

Last edited by era; 10-03-2008 at 02:58 AM.. Reason: Remove -w if word-internal occurrences should count as well
Sponsored Links
    #6  
Old Unix and Linux 10-03-2008
madhu.it madhu.it is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 11 October 2008, 5:10 AM EDT
Location: andhra pradesh
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
thanks ....its working.....
Sponsored Links
    #7  
Old Unix and Linux 10-03-2008
madhu.it madhu.it is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 11 October 2008, 5:10 AM EDT
Location: andhra pradesh
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Unix or Linux Question to extract a sub-string from a main string

i need a shell script progrm...to extract a sub-string from a string...
foe eg:-
my string is madhu
i want to extract sub-string from this .my sub string is "mad"..
i want the out put will be a sub string..
0/p is mad......
pls give this answer..

mad from madhu....extracting substring from a string

Last edited by madhu.it; 10-03-2008 at 06:10 AM..
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to count number of times each word exist in a file shnkool UNIX for Dummies Questions & Answers 4 12-07-2011 04:54 AM
Print a word specific number of times sam_2921 Shell Programming and Scripting 4 05-14-2010 07:06 AM
Looking for a single line to count how many times one character occurs in a word... Shingoshi Shell Programming and Scripting 2 06-12-2009 10:12 PM
delete lines containing a specific word in afile sireesha9 Shell Programming and Scripting 4 02-23-2009 05:07 AM
TO find the word which occurs maximum number of times aajan Shell Programming and Scripting 5 01-11-2008 03:11 AM



All times are GMT -4. The time now is 07:47 PM.