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 the number of occurence of perticular word from file

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-25-2007
rinku rinku is offline
Registered User
 
Join Date: May 2007
Last Activity: 4 September 2008, 2:38 AM EDT
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Data 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. Linux
Sponsored Links
    #2  
Old Unix and Linux 05-25-2007
blowtorch's Unix or Linux Image
blowtorch blowtorch is offline Forum Advisor  
AFK
 
Join Date: Dec 2004
Last Activity: 11 July 2015, 4:56 AM EDT
Location: UK
Posts: 2,351
Thanks: 0
Thanked 6 Times in 6 Posts
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.
Sponsored Links
    #3  
Old Unix and Linux 05-25-2007
funksen funksen is offline Forum Advisor  
Registered User
 
Join Date: Nov 2006
Last Activity: 12 August 2015, 4:10 AM EDT
Location: Austria/Vienna
Posts: 677
Thanks: 22
Thanked 31 Times in 30 Posts
grep -o <string> <file> | wc -w
    #4  
Old Unix and Linux 05-25-2007
cfajohnson's Unix or Linux Image
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 27 July 2015, 12:41 PM EDT
Location: Toronto, Canada
Posts: 2,896
Thanks: 0
Thanked 131 Times in 116 Posts
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"

The Following 2 Users Say Thank You to cfajohnson For This Useful Post:
Rajeev Ranjan K (06-30-2014), Siddharth Gee (06-04-2013)
Sponsored Links
    #5  
Old Unix and Linux 08-08-2007
pelipeplips pelipeplips is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 16 November 2008, 9:16 PM EST
Location: Manila, Philippines
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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

Sponsored Links
    #6  
Old Unix and Linux 08-08-2007
Klashxx's Unix or Linux Image
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
 
Join Date: Feb 2006
Last Activity: 24 July 2015, 7:39 AM EDT
Location: Almerķa, Spain
Posts: 779
Thanks: 24
Thanked 112 Times in 106 Posts
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

Sponsored Links
    #7  
Old Unix and Linux 08-08-2007
Shell_Life's Unix or Linux Image
Shell_Life Shell_Life is offline
Registered User
 
Join Date: Mar 2007
Last Activity: 21 June 2012, 3:19 PM EDT
Location: Bahia, Brazil
Posts: 1,203
Thanks: 1
Thanked 103 Times in 100 Posts
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 Linux or Unix Question

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
Count number of character occurence but not from quotation marks calinlicj Shell Programming and Scripting 3 02-03-2012 10:07 AM
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
Count number of occurences of a word shikhakaul UNIX for Dummies Questions & Answers 8 06-25-2009 11:10 AM
finding the number of occurence of a word in a line priyanka3006 Shell Programming and Scripting 9 06-18-2009 07:55 AM
search& count for the occurence of a word skoppana UNIX for Dummies Questions & Answers 1 11-09-2007 04:07 PM



All times are GMT -4. The time now is 10:24 PM.