Count number of occurences of a word


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Count number of occurences of a word
# 1  
Old 11-28-2007
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  
Old 11-28-2007
Code:
grep -cw 200 filename

# 3  
Old 11-29-2007
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  
Old 11-29-2007
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. Smilie

To make it clear, OP has to publish sample data Smilie
# 5  
Old 11-29-2007
Error 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  
Old 11-29-2007
Try

Code:
grep -c "pdf 200"  filename

# 7  
Old 11-29-2007
Quote:
Originally Posted by fpmurphy
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)
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Count occurences of the word without it repeating

Hi, I would like to count the number of ALA occurences without having them to be repeated. In the script I have written now it has 40 repetitions of ALA but it has to be 8. ALA is chosen as one of the 20 values it can have when the script asks for the input of AAA, which for this example is chosen... (7 Replies)
Discussion started by: Aurimas
7 Replies

2. Shell Programming and Scripting

Count and print the number of occurences

I have some files as shown below GLL ALM 654-656 654 656 SEM LYG 655-657 655 657 SEM LYG 655-657 655 657 ALM LEG 656-658 656 658 ALM LEG 656-658 656 658 ALM LEG 656-658 656 658 LEG LEG 658-660 658 660 LEG LEG 658-660 658 660 The value of GLL is... (5 Replies)
Discussion started by: arch
5 Replies

3. Shell Programming and Scripting

Count number of occurences using awk

Hi Guys, I have 2 files like below file1 xx yy file2 b yy b2 xx c1 yy xx yy Now I want an idea which can count occurences of text from file1 and file2 so outbout would be kind of (9 Replies)
Discussion started by: prashant2507198
9 Replies

4. UNIX for Dummies Questions & Answers

Number of word occurences in a file?

Hello world, Can anybody tell me how to count how many times does a word repeat in a file? There have been many threads on this but they all are heavy loads of Scripting for a starter like me. :D So, I sat down today and after some hours of reading man pages, I found a simple one-line... (18 Replies)
Discussion started by: satish51392111
18 Replies

5. Shell Programming and Scripting

to count the number of occurences of a column value

im trying to count the number of occurences of column 2 value(starting from KKK*) of the below file, file.txt using the code cat file.txt | awk ' BEGIN { print "Category Counts"} {FS=","} {NR > 2} { cats = cats + 1} END { for(c in cats) { print c, "=", cats} } ' but its returning as ... (6 Replies)
Discussion started by: michaelrozar17
6 Replies

6. Shell Programming and Scripting

Count the number of occurrences of the word

I am a newbie in UNIX shell script and seeking help on this UNIX function. Please give me a hand. Thanks. I have a large file. Named as 'MyFile'. It was tab-delmited. I am told to write a shell function that counts the number of occurrences of the ord “mysring” in the file 'MyFile'. (1 Reply)
Discussion started by: duke0001
1 Replies

7. Shell Programming and Scripting

Count number of digits in a word

Hi all Can anybody suggest me, how to get the count of digits in a word I tried WORD=abcd1234 echo $WORD | grep -oE ] | wc -l 4 It works in bash command line, but not in scripts :mad: (12 Replies)
Discussion started by: ./hari.sh
12 Replies

8. Shell Programming and Scripting

How to count the number of occurences of this pattern?

Hi all, I have a pattern like this in a file: 123 4 56 789 234 5 67 789 121 3 56 789 222 4 65 789 321 6 90 100 478 8 40 789 243 7 80 789 How can I count the number of occurences of '789' (4th column) in this set...? Thanks for all your help! K (7 Replies)
Discussion started by: kripssmart
7 Replies

9. UNIX for Dummies Questions & Answers

How to count the occurences of a specific word in a file in bash shell

Hello, I want to count the occurences of a specific word in a .txt file in bash shell. Can somebody help me pleaze?? Thanks!!! (2 Replies)
Discussion started by: mskart
2 Replies

10. UNIX for Dummies Questions & Answers

word count showing wrong number of lines

Hi , I am using SUN OS Version 5.6. I have a file that contains records of length 270. when I do 'set nu' in vi editor, I get the count as 86. whereas when I do "wc -l" on the command prompt, it shows the count as only 85. this is very strange. why would the 'wc' show 1 record less. The job... (3 Replies)
Discussion started by: tselvanin
3 Replies
Login or Register to Ask a Question