Count pattern occurences


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Count pattern occurences
# 1  
Old 01-19-2012
Count pattern occurences

hi,
I have a text..and i need to find a pattern in the text and count to the no of times the pattern occured.

i have used grep command ..but the problem is , it shows the occurrences of the pattern but doesn't count no of times the pattern occuries.
# 2  
Old 01-19-2012
Try using:

Code:
grep -c 'pattern' file.txt

The -c just returns the number of occurrences.
# 3  
Old 01-19-2012
Hi.

More specifically: print a count of matching lines (excerpt from man page for GNU grep), which often will be sufficient, but if the number of matches is important, and if your data has more than one match per line, then the solution is more complicated ... cheers, drl
# 4  
Old 01-21-2012
here if u use grep -c 'pattern' file.txt ..it doesn't count the exact no of occurrences of a pattern.. like in a paragraph...if the text occurs five to six times ..it doesnot print 5 or 6...it just prints 1.
# 5  
Old 01-21-2012
Code:
cat >temp
govind govindgovind govind
 
cat temp|sed 's/govind/-/g'|tr -d "\n"|wc -c
4

Moderator's Comments:
Mod Comment Please use [CODE][/CODE] tags where appropriate, thank you

Last edited by vbe; 01-21-2012 at 09:21 AM..
# 6  
Old 01-21-2012
Try:
Code:
grep -o 'pattern' infile | wc -l

-or-
Code:
awk '{t+=gsub(/pattern/,x)}END{print t}' RS= infile

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count the occurences of strings

I have some text files in a folder f1 with 10 columns. The first five columns of a file are shown below. aab abb 263-455 263 455 aab abb 263-455 263 455 aab abb 263-455 263 455 bbb abb 26-455 26 455 bbb abb 26-455 26 455 bbb aka 264-266 264 266 bga bga 230-232 230 ... (10 Replies)
Discussion started by: gomez
10 Replies

2. Shell Programming and Scripting

awk count occurences

line number:status, market, keystation 1,SENT,EBS,1 : 1 2,DONE,REU,1 : 1 3,SENT,EBS,2 : 1 4,DONE,EBS,1 : 0 5,SENT,EBS,2 : 0 6,SENT,EBS,2 : 0 7,SENT,EBS,2 : 0 8,SENT,EBS,1 : 1 for each status, market combination I want to keep a tally of active orders. i.e if an order is SENT, then +1, if... (8 Replies)
Discussion started by: Calypso
8 Replies

3. Shell Programming and Scripting

Count occurences based on interval

Hi, I have a file which has 4500 entries 10000 9880 9800 8700 8200 ... ..... ... ... ... ... ... ... ... 50 (1 Reply)
Discussion started by: Diya123
1 Replies

4. Shell Programming and Scripting

Count of matched pattern occurences by minute and date in a log file

Anyone knows how to use AWK to achieve the following Sun Feb 12 00:41:01-00:41:59 Success:2 Fail:2 Sun Feb 12 00:42:01-00:42:59 Success:1 Fail:2 Sun Feb 12 01:20:01-01:20:59 Success:1 Fail:2 Mon Feb 13 22:41:01-22:41:59 Success:1 Fail:1 log file: Success Success Fail Fail ... (9 Replies)
Discussion started by: timmywong
9 Replies

5. Shell Programming and Scripting

Count occurences of string

Hi, Please help me in finding the number of occurences of the string. Example: Apple, green, blue, Apple, Orange, green, blue are the strings can be even in the next line. The o/p should look as: Word Count ----- ----- Apple 2 green 2 Orange 1 blue 2 Thanks (2 Replies)
Discussion started by: acc888
2 Replies

6. Shell Programming and Scripting

Count of matched pattern occurences by time in a log file

We have a log file, the format is similar to this: 08/04/2011 05:03:08 Connection Success 08/04/2011 05:13:18 Connection Success 08/04/2011 05:23:28 Connection Fail 08/04/2011 05:33:38 Connection Success 08/04/2011 06:14:18 Connection Success 08/04/2011 06:24:28 Connection Fail 08/04/2011... (6 Replies)
Discussion started by: clu
6 Replies

7. Shell Programming and Scripting

Awk to count occurences

Hi, i am in need of an awk script to accomplish the following: Input table looks like: Student1 arts Student2 science Student3 arts Student4 science Student5 science Student6 science Student7 science Student8 science Student9 science Student10 science Student11 science... (8 Replies)
Discussion started by: saint2006
8 Replies

8. UNIX for Dummies Questions & Answers

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!!!!! (8 Replies)
Discussion started by: shikhakaul
8 Replies

9. Shell Programming and Scripting

Perl - Count occurences

I have enclosed the script. I am able to find the files that contain my search string but when I try to count the occurences within the file I get zero always. Any help on this. #!/usr/bin/perl my $find = $ARGV; my $replace = $ARGV; my $glob = $ARGV; @filelist = <*$glob>; # process each... (22 Replies)
Discussion started by: TimHortons
22 Replies

10. 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
Login or Register to Ask a Question