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 Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-25-2007
rinku rinku is offline
Registered User
  
 

Join Date: May 2007
Posts: 54
Unhappy Count the number of occurence of perticular word from file

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.
  #2 (permalink)  
Old 05-25-2007
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
The 'less' command is used to view a file. Use grep to search for a particular word in a file. You can use this to count the number of occurrences too, just check the man page for the exact switch.
  #3 (permalink)  
Old 05-25-2007
funksen funksen is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2006
Location: Austria/Vienna
Posts: 430
grep -o <string> <file> | wc -w
  #4 (permalink)  
Old 05-25-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 rinku
I want to count the number of occurence of perticular word from one text file.

Code:
tr -cs 'A-Za-z' '\n' < FILE | grep -c "WORD"
  #5 (permalink)  
Old 08-08-2007
pelipeplips pelipeplips is offline
Registered User
  
 

Join Date: Aug 2007
Location: Manila, Philippines
Posts: 3
Quote:
grep -o <string> <file> | wc -w
I tried using grep with the "-o" option and it gives me this error:
grep: illegal option -- o
What does "-o" option do?

I also need to find an occurence of a certain string within a file. Currently I'm using:
Code:
grep -c 'abc' sample.txt
But the code above only counts the occurrences per line. How will i get the total count of the 'abc' words regardless of how many occerence they have per line?

Example:
This is line 1 abc and abc
This is line 2 abc

  #6 (permalink)  
Old 08-08-2007
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
  
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 393
Use:
Code:
awk '{ 
     for (i=1;i<=NF;i++)
         if ( $i == "abc")
         c++
     }
END{
print c}' sample.txt
Or:
Code:
awk '
BEGIN {
RS=FS
}
{
if ( $0 ~ /abc/ )
   c++
}
END{
print c++
}' lsample.txt
  #7 (permalink)  
Old 08-08-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Quote:
Originally Posted by cfajohnson View Post

Code:
tr -cs 'A-Za-z' '\n' < FILE | grep -c "WORD"
This solution does not work.

Here is a sample file:
Code:
a aa aaa
aaa aa a
aaa aa a aaa aa a aaa
Here is one test:
Code:
tr -cs 'A-Za-z' '\n' < FILE | grep -c "aaa"
It gives the total of words as '3', when the answer is '5'.

Here is another possible solution for those who want to use shell script:
Code:
#!/bin/ksh
typeset -i mCnt=0
mWord='aaa'
for mEach in `cat input_file`
do
  if [ "${mEach}" = "${mWord}" ]; then
    mCnt=${mCnt}+1
  fi
done
echo 'Total words for '${mWord}' = '${mCnt}

Last edited by Shell_Life; 08-08-2007 at 11:14 AM..
Sponsored Links
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 05:55 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