To get the total of how many times the pattern is repeated.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers To get the total of how many times the pattern is repeated.
# 1  
Old 09-20-2010
To get the total of how many times the pattern is repeated.

Hi,

I need the total of how many times the pattern is being repeated in a particular file.

I tried with grep as below, but no go.

Code:
grep -c best sample

Contents of Sample file:
-----------------------
This is the best site I have never come across. The best one indeed.
Very best site to post queries.

Out put I got is 2, am expecting 3, as the pattern "best" is being repeated for 3 times.

Many thanks.

Rgds,
# 2  
Old 09-20-2010
Code:
$ cat file
This is the best site I have never come across. The best one indeed.
Very best site to post queries.

$  ruby -00 -ne 'puts $_.scan(/best/).size' file
3

# 3  
Old 09-20-2010
Code:
grep -o best sample|wc -l

# 4  
Old 09-20-2010
# 5  
Old 09-20-2010
Assuming "pattern" means a whole word within a sentence and that the only delimiters are "space" and "full stop" we can break the lines up such that each word is a separate line then use "grep -x" for an exact match thereby avoiding false matches with words like "asbestos".

Code:
cat sample.txt | tr '.' ' ' | tr -s ' '|tr ' ' '\n' | grep -x "best" | wc -l

3

# 6  
Old 09-20-2010
Thanks a lot rdcwayx and methyl.
# 7  
Old 09-21-2010
Any idea, why my below command is not working/giving me the expected output?

Code:
perl -e '$cnt=0; while (<>) { while (m/\bpattern\b/g) {$cnt++}} print $cnt "\n"' file

assume, the file has enough no of patterns available and I am trying to count the no of occurrences of the pattern

Last edited by royalibrahim; 09-22-2010 at 03:05 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove duplicate lines which has been repeated 4 times

Remove duplicate lines which has been repeated 4 times attached test.txt below command tried and not getting expect output. for i in `cat test.txt | uniq` do num=`cat test.txt | grep $i | wc -l` echo $i $num done test.txt ... (17 Replies)
Discussion started by: Kalia
17 Replies

2. UNIX for Beginners Questions & Answers

Export lines that have first entry repeated 5 times or above

Dears i want to extract lines only that have first entry repeated 3 times or above , ex data : -bash-3.00$ cat INTCONT-IS.CSV M205-00-106_AMDRN:1-0-6-22,12-662-4833,intContact,2016-11-15 02:32:16,50 M205-00-106_AMDRN:1-0-23-17,12-616-0462,intContact,2016-11-15 02:32:23,50... (5 Replies)
Discussion started by: is2_egypt
5 Replies

3. UNIX for Dummies Questions & Answers

Append no of times a column is repeated at the end

Hi folks, Iam working on a bash script, i need to print how many times column 2 repeated at the end of each line. Input.txt COL1 COL2 COL3 COL4 1 XX 45 N 2 YY 34 y 3 ZZ 44 N 4 XX 89 Y 5 XX 45 N 6 YY 84 D 7 ZZ 22 S Output.txt COL1 COL2 COL3 COL4 COL5 1 XX 45 N 3 2 YY 34... (6 Replies)
Discussion started by: tech_frk
6 Replies

4. Homework & Coursework Questions

Accepting a phrase and counting the number of times that it is repeated in a specific website

1. The problem statement, all variables and given/known data: Develop a shell script that accepts a phrase and counts the number of times that it is repeated in a specific website. Note: Im not sure if it's the whole website, or just a specific page but im guessing its thewhole website. ... (2 Replies)
Discussion started by: Zakerii
2 Replies

5. Shell Programming and Scripting

How to print the lines which are repeated 3 times in a file?

Hello All, I have a file which has repeated lines. I want to print the lines which are repeated three times. Please help. (3 Replies)
Discussion started by: ailnilanjan
3 Replies

6. Shell Programming and Scripting

need to print lines between repeated pattern

Hi all, I have a file that looks like this: uid=bessemsj version: 1 dn: cn=Desk SpecialAdminDesk, ou=Desks, dc=DSS,c=nl,o=Vodafone dn: cn=DSS Advisors, ou=Groups, dc=DSS,c=nl,o=Vodafone dn: cn=DSS Dispatcher,ou=Groups,dc=DSS,c=nl,o=Vodafone dn: cn=Desk Retention Desk,ou=Desks,... (13 Replies)
Discussion started by: Eman_in_forum
13 Replies

7. UNIX for Dummies Questions & Answers

awk -repeated pattern match

Hi. How can I write this differently: awk '$3 ~ /0001/{print}' Is there a way to write 0001 differently. I am looking for the pattern 01, with 3 or more 0 and 3 or more 1 in a pattern. Thanks. (12 Replies)
Discussion started by: danieladna
12 Replies

8. UNIX for Dummies Questions & Answers

Extracting column if above certain values and repeated over a number of times continuously

Hi I am new to the forum and would like to ask: i have a file in form with thousands of column id.1 A01 A01 A68 A68 id.2 A5 A5 A3 A3 1001 0 0 0.136 0.136 1002 0 0 0.262 0.183 1003 0 0 0.662 0.662 1004 0 0 ... (9 Replies)
Discussion started by: newbeeuk
9 Replies

9. Shell Programming and Scripting

Repeated Pattern

I have a list like this: todu todo tofe tafo I want to grep only the lines where the 2nd and the 4th character are the same. In this case I would get only "todo". Thanks. (3 Replies)
Discussion started by: rlopes
3 Replies

10. UNIX for Dummies Questions & Answers

how many times does this repeated sequence exist

need a script to determine daily how many times does the below repeated sequence exist in a log file, and if it shows failure to connect in the same file 200 PORT Command successful. 150 Opening BINARY mode data connection for rgr016.daily.0305. 226 Transfer complete. local: rgr016.daily.0306... (3 Replies)
Discussion started by: lichento
3 Replies
Login or Register to Ask a Question