The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Count number of occurences of a word shikhakaul UNIX for Dummies Questions & Answers 8 06-25-2009 11:10 AM
How to find a count of a word within a file bd_joy Shell Programming and Scripting 9 07-14-2008 09:29 AM
grep all records in a file and get a word count -perl meghana Shell Programming and Scripting 4 02-13-2008 10:06 PM
search& count for the occurence of a word skoppana UNIX for Dummies Questions & Answers 1 11-09-2007 05:07 PM
word count showing wrong number of lines tselvanin UNIX for Dummies Questions & Answers 3 01-06-2004 11:33 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-09-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by Shell_Life View Post
Using the same test data:
Code:
a aa aaa
aaa aa a
aaa aa a aaa aa a aaa
This solution does not work for the following case:
Code:
tr -s ' \011\012' '\n\n\n' < FILE | grep -c "a"
It gives the total of words as '13', when the answer is '4'.

Then you need to adjust your grep to search for a word, not a letter. If your grep has it, use -w; if not, use "\<a\>".

Also, some older versions of tr need \012 instead of \n.
  #2 (permalink)  
Old 08-08-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,533
Quote:
Originally Posted by rinku View Post
I want to count the number of occurence of perticular word from one text file.

Please tell me "less" command is work in ksh or not. If it is not working then instead of that which command will work.
See here for example
  #3 (permalink)  
Old 08-10-2007
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2007
Location: Bangalore
Posts: 377
Thumbs up

Here is the command which you can use-

grep -o "string" filename | wc -w
  #4 (permalink)  
Old 08-10-2007
fazliturk fazliturk is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 45
try this
count1=`wc -w inputfile`
count2=`sed s/word/" "/g inputfile |wc -w`

/*number of word =count1-count2*/
  #5 (permalink)  
Old 08-10-2007
kahuna's Avatar
kahuna kahuna is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 149
Quote:
Originally Posted by fazliturk View Post
count1=`wc -w inputfile`
count2=`sed s/word/" "/g inputfile |wc -w`

/*number of word =count1-count2*/
I don't think this works if the word is in the middle of a larger word.
Code:
$echo compoundwordinmiddle >inputfile      
$count1=`wc -w inputfile`                  
$count2=`sed s/word/" "/g inputfile |wc -w`
$echo $count1
1 inputfile
$echo $count2
2
  #6 (permalink)  
Old 08-10-2007
fazliturk fazliturk is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 45
Quote:
Originally Posted by kahuna View Post
I don't think this works if the word is in the middle of a larger word.
Code:
$echo compoundwordinmiddle >inputfile      
$count1=`wc -w inputfile`                  
$count2=`sed s/word/" "/g inputfile |wc -w`
$echo $count1
1 inputfile
$echo $count2
2
yes you are right but if not this code is very simple
  #7 (permalink)  
Old 08-10-2007
kahuna's Avatar
kahuna kahuna is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 149
Quote:
Originally Posted by fazliturk View Post
yes you are right but if not this code is very simple
"echo 1" is very simple, but also not correct
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:05 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0