How to count the pattern in a file by awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to count the pattern in a file by awk
# 1  
Old 08-05-2010
How to count the pattern in a file by awk

hello everybody,
I have 3 files
eg-
Code:
     sample1
     sample2 
     sample3

each file contain word babu many times

eg-
Code:
cat sample1
babu amit msdfmdfkl babu abhi
babu ruby 
amit babu

I want to count only the count of babu ,how many times it appeared .
any help will be appreciated.....................

Last edited by zaxxon; 08-05-2010 at 11:54 AM..
# 2  
Old 08-05-2010
For a single file:
Code:
$> cat infile
lala baba lalala baba s
aaj baba
baba sazh23r baba 09q8wfh2983f baba
$> awk '{for(x=1; x<=NF;x++){if($x ~ /baba/){z++}}} END{print z}' infile
6

If you want to count it for more files you can just add the filenames and get a sum of all matches.
# 3  
Old 08-05-2010
Code:
cat /tmp/t1
one
two
two
one
three

sort t1 | uniq -c  or to take it even further sort t1 | uniq -c | grep two


Last edited by Scott; 08-06-2010 at 05:22 AM.. Reason: Please use code tags
# 4  
Old 08-05-2010
Code:
sed -n 's/\(babu\)/\1\n/pg' file1 file2 file3 | grep babu|wc -l

# 5  
Old 08-05-2010
Quote:
Originally Posted by kurumi
Code:
sed -n 's/\(babu\)/\1\n/pg' file1 file2 file3 | grep babu|wc -l

In case this fails for someone, it's probably due to \n in the replacement text being a gnu extension.

---------- Post updated at 03:42 PM ---------- Previous update was at 03:40 PM ----------

Another awk alternative:
Code:
awk -F babu 'NF{x+=NF-1} END {print x}' file

Note: babubabu would count as two instances of babu instead of being ignored as a different word.
# 6  
Old 08-06-2010
thanks a lot !!!!!! alister
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 Pattern using awk

I need to get a count of all the records that start with 4 and then print the value. I have the below statement but it is missing something, can someone help me to fix this awk 'BEGIN{~/^4/{C++}};END {print"<Total>"} {print"<Reg>"}{print "<value>"C"</value></Reg>"}' {print"</Total>"} temp >... (2 Replies)
Discussion started by: rosebud123
2 Replies

2. Shell Programming and Scripting

Pattern count in file

hi , I have a below file which contain as Use descriptive thread titles when posting Urgent. For example, do not post questions with subjects like "Help Me!", "Urgent Urgent Urgent" . or "Doubt". Post subjects like "Execution Problems with Cron" or "Help with Backup Shell Script".... (7 Replies)
Discussion started by: Jewel
7 Replies

3. Shell Programming and Scripting

Identify file pattern, take count of pattern, then act

Guys - Need your ideas on a section of code to finish something up. To make a long story short, I'm parsing a print output file that goes to pre-printed forms. I'm intercepting it, parsing it, formatting it, cutting it up into individual pages, grabbing the text I want in zones, building an... (3 Replies)
Discussion started by: ampsys
3 Replies

4. Shell Programming and Scripting

Combine 4 awk pattern count statements into 1

Hello fellow awkers, I am trying to combine the following awk statements into 1 so that the results are more accurate: awk '/\=\+/ { count++ } END { print count}' filename awk '/\=\?/ { count++ } END { print count}' filename awk '/\=\-/ { count++ } END { print count}' filename awk... (8 Replies)
Discussion started by: ux4me
8 Replies

5. Shell Programming and Scripting

awk pattern match and count unique in column

Hi all I have a need of searching some pattern in file by month and then count unique records D11 G11 R11 -------> Pattern available in file S11 Jan$1 to $5 column contains some records in which I want to find unique for this purpose I have written script like below awk '/Jan/ ||... (4 Replies)
Discussion started by: nex_asp
4 Replies

6. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

7. Shell Programming and Scripting

Checking a pattern in file and the count of characters

I am having a zipped file which has the following URL contents - 98.70.217.222 - - "GET /liveupdate-aka.symantec.com/1340071490jtun_nav2k8enn09m25.m25?h=abcdefgh HTTP/1.1" 200 159229484 "-" "hBU1OhDsPXknMepDBJNScBj4BQcmUz5TwAAAAA" "-" In this line here is we only need to consider the... (4 Replies)
Discussion started by: Naks_Sh10
4 Replies

8. Shell Programming and Scripting

Multiple pattern matching using awk and getting count of lines

Hi , I have a file which has multiple rows of data, i want to match the pattern for two columns and if both conditions satisfied i have to add the counter by 1 and finally print the count value. How to proceed... I tried in this way... awk -F, 'BEGIN {cnt = 0} {if $6 == "VLY278" &&... (6 Replies)
Discussion started by: aemunathan
6 Replies

9. Shell Programming and Scripting

Awk Count Pattern problem.

I want to keep a count of a all the records processed in a input file. The input file would have a lot of data containing various information. Lets say I make a pattern that only prints out data with the amount $37.57. How would I go about keeping track of how many $37.57 appears? I have... (2 Replies)
Discussion started by: Boltftw
2 Replies

10. Shell Programming and Scripting

awk to count pattern matches

i have an awk statement which i am using to count the number of occurences of the number ,5, in the file: awk '/,5,/ {count++}' TRY.txt | awk 'END { printf(" Total parts: %d",count)}' i know there is a total of 10 matches..what is wrong here? thanks (16 Replies)
Discussion started by: npatwardhan
16 Replies
Login or Register to Ask a Question