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 > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
count occurences of specific character in the file superprogrammer HP-UX 9 04-09-2008 11:05 AM
Count the number of occurence of perticular word from file rinku Shell Programming and Scripting 40 08-10-2007 07:33 PM
grep and count no of occurences in every line of a file srikanthgr1 Shell Programming and Scripting 12 04-20-2007 09:16 AM
How to count no of occurences of a character in a string in UNIX kamesh83 UNIX for Advanced & Expert Users 11 03-17-2006 02:39 PM
word count showing wrong number of lines tselvanin UNIX for Dummies Questions & Answers 3 01-06-2004 11:33 PM

Reply
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 Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 11-28-2007
shikhakaul shikhakaul is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 11
Question Count number of occurences of a word

I want to count the number of occurences of say "200" in a file but that file also contains various stuff including dtaes like 2007 or smtg like 200.1 so count i am getting by doing grep -c "word" file is wrong
Please help!!!!!
  #2 (permalink)  
Old 11-28-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
Code:
grep -cw 200 filename
  #3 (permalink)  
Old 11-29-2007
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
using Perl:
Code:
#!/usr/bin/perl
# search_word.pl
my $search_this = shift;
my $count       = 0;
while (<>) {
    while (m/\b$search_this\b/g) {
        $count++;
    }
}
if ($count == 0) {
    print $ARGV . "does not contain " . $search_this . "\n";
}
else {
    print $ARGV . "contains " . $search_this . " " . $count . " times \n";
}
run this script as :
Code:
$ perl search_word.pl 200 filename
  #4 (permalink)  
Old 11-29-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
I thought OP was interested in the count of pattern ( that are to be considered as complete patterns ) and not the number of such patterns that might occur in the same line

Well, this is possible.

To make it clear, OP has to publish sample data
  #5 (permalink)  
Old 11-29-2007
shikhakaul shikhakaul is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 11
Exclamation This time with example

cpliterature_23289.esw3c_S.200710260000-2400-0:2007-10-25 23:56:52 72.30.216.103 GET /cpliterature.product.agilent.com/litweb/pdf/5989-4075ZHA.pdf 200 505286 72 "-" "Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)" "-"
cpliterature_23289.esw3c_S.200710260000-2400-0:2007-10-25 23:57:14 74.6.28.142 GET /cpliterature.product.agilent.com/litweb/pdf/81130-91220.pdf 200 2888156 320 "-" "Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)" "-"
cpliterature_23289.esw3c_S.200710260000-2400-0:2007-10-26 00:00:03 74.6.19.209 GET /cpliterature.product.agilent.com/litweb/pdf/5988-4969EN.pdf 200 1905042 156 "-" "Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)" "-"


My file contains data like given above.....
I have to find number of times 200 appears in this file but only that 200 which comes after pdf
  #6 (permalink)  
Old 11-29-2007
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,910
Try

Code:
grep -c "pdf 200"  filename
  #7 (permalink)  
Old 11-29-2007
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
Quote:
Originally Posted by fpmurphy View Post
Try

Code:
grep -c "pdf 200"  filename
this would work in the case that we have, where pattern "pdf 200" exists at only one place on a line. I was under impression that the pattern may exist multiple times on the same line.

My solution was aimed at that, which would give the exact count (grep would give incorrect count, since it counts the number of lines on which the pattern is found)
Sponsored Links
Reply

Bookmarks

Tags
grep, perl, perl shift, shift, shift perl

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 08:51 AM.


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