Count occurance of multiple strings using grep command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count occurance of multiple strings using grep command
# 1  
Old 12-10-2009
Count occurance of multiple strings using grep command

How to grep multiple string occurance in input file using single grep command? I have below input file with many IDP, RRBE messages. Out put should have count of each messages.

I have used below command but it is not working
grep -cH "(sent IDP Request)(Recv RRBCSM)" *.txt >>Sigtran_Errorlogs.txt

InputfileSmiliebelow messages present multiple times)
[14:39:10 10/12/2009]:: SStub-->ICC: otId[1218] sent IDP Request
[14:39:10 10/12/2009]:: ICC-->SStub: otId[2015304695] dtId[10] Recv RRBCSM
[14:39:10 10/12/2009]:: ICC-->SStub: otId[2015304695] dtId[10] Recv ACH
[14:39:10 10/12/2009]:: ICC-->SStub: otId[2015304695] dtId[10] Recv CONNECT/CONTINUE Request
[14:39:10 10/12/2009]:: SStub-->ICC: otId[10] dtId[2015304695] sent ERB Answer
[14:39:10 10/12/2009]:: SStub-->ICC: otId[10] dtId[2015304695] sent ERB Disconnect
[14:39:10 10/12/2009]:: SStub-->ICC: otId[10] dtId[2015304695] sent ACR
[14:39:10 10/12/2009]:: SStub-->ICC: otId[10] dtId[2015304695] sent TC_END

Outputfile (Sigtran_Errorlogs.txt) should look like:
150 capMessageLog.txt.log: sent IDP Request
100 capMessageLog.txt.log: Recv RRBCSM
# 2  
Old 12-10-2009
Something like this?

Code:
awk '
/sent IDP Request/{i++}
/Recv RRBCSM/{j++}
END{
  print i " capMessageLog.txt.log: sent IDP Request"}
  print j " capMessageLog.txt.log: Recv RRBCSM"
}' file

# 3  
Old 12-10-2009
This method will be effective is i have only 1 input file but I have many input log files. If you send same command using grep for multiple files it will be more useful
# 4  
Old 12-10-2009
Quote:
Originally Posted by sushmab82
This method will be effective is i have only 1 input file but I have many input log files. If you send same command using grep for multiple files it will be more useful
Like this?

Code:
awk '
/sent IDP Request/{i++}
/Recv RRBCSM/{j++}
END{
  print i " capMessageLog.txt.log: sent IDP Request"}
  print j " capMessageLog.txt.log: Recv RRBCSM"
}' *.txt >> Sigtran_Errorlogs.txt

# 5  
Old 12-10-2009
You can do this or that, but you cannot do the both.

Code:
grep -cH "\(sent IDP Request\)\|\(Recv RRBCSM\)"

This will show the count of both matches.
But if you want it in a different count, you have to use more than one grep, i dont believe anyother way is exists with single grep ?

Why do you want it to be achieved in single grep ?!
# 6  
Old 12-10-2009
I was just seaching for similar commond like below which I used for greping numbers. Since here matched string line is not uniq it is not printing total count for matched string in given input file.

grep -H "\[[345]0[0123][0123456789]\]" *.log | sort | uniq -c >>Dia_Errorlogs.txt

Out put file will look like this:
161519 ccr-cca.2009-12-08.11-59-51.799.clientGPRS_PostPaid_LatestV1.log: |M| (avp-code = 268)(flags = 64)] = [5030]
408 ccr-cca.2009-12-08.11-59-51.799.clientGPRS_PostPaid_LatestV1.log: |M| (avp-code = 268)(flags = 64)] = [5031]

I was looking for similar command for string which should not check for uniqueness of line
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

awk to count occurrence of strings and loop for multiple columns

Hi all, If i would like to process a file input as below: col1 col2 col3 ...col100 1 A C E A ... 3 D E G A 5 T T A A 6 D C A G how can i perform a for loop to count the occurences of letters in each column? (just like uniq -c ) in every column. on top of that, i would also like... (8 Replies)
Discussion started by: iling14
8 Replies

2. Shell Programming and Scripting

Grep multiple strings in a file

Consider i have the below data in my log file. i want to grep using "Monday" and "Working" So the only output i expect is Can you help me with the grep query for Sun Sparc ? Usage: grep -hblcnsviw pattern file . . . (8 Replies)
Discussion started by: mohtashims
8 Replies

3. Shell Programming and Scripting

Extract multiple occurance of strings between 2 patterns

I need to extract multiple occurance strings between 2 different patterns in given line. For e.g. in below as input ------------------------------------------------------------------------------------- mike(hussey) AND mike(donald) AND mike(ryan) AND mike(johnson)... (8 Replies)
Discussion started by: sameermohite
8 Replies

4. Shell Programming and Scripting

Can't grep multiple strings

I have a script that periodically checks the Apache error_log to search for a specific error that causes it to hand and, if found, it restarts the service. I recently found another error that forces it to hand and won't serve pages until it is reset. What I'm trying to do is to get the script to... (3 Replies)
Discussion started by: cfjohnsn
3 Replies

5. Shell Programming and Scripting

Grep multiple strings in multiple files

Hi, every one! I have a file with multiple strings. file1 ATQRGNE ASQGVKFTE ASSQYRDRGGLET SPEQGARSDE ASSRDFTDT ASSYSGGYE ASSYTRLWNTGE ASQGHNTD PSLGGGNQPQH SLDRDSYNEQF I want to grep each string in hundreds of files in the same directory, further, I want to find out the string... (7 Replies)
Discussion started by: xshang
7 Replies

6. UNIX for Dummies Questions & Answers

Grep multiple strings in multiple files using single command

Hi, I will use below command for grep single string ("osuser" is search string) ex: find . -type f | xarg grep -il osuser but i have one more string "v$session" here i want to grep in which file these two strings are present. any help is appreciated, Thanks in advance. Gagan (2 Replies)
Discussion started by: gagan4599
2 Replies

7. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

8. Shell Programming and Scripting

Grep Multiple Strings

Hi, Can any one pelase tell me how to grep multiple strings from multiple files in a singel folder? grep -E "string1|string2|string3|string4|string..." its taking lots of time.. can any please tell me fast grep??? URGENT (10 Replies)
Discussion started by: durgaprasad
10 Replies

9. Shell Programming and Scripting

Efficient way to grep multiple strings

I have a script which searches a huge log file for the existence of a specified string and if the string is not present i receive an alert mail. Here's an extract: STRING=$(grep 'warning' logfile | tail -1 | wc -l) if (( ${STRING} > 0 )); then print -- "---- Warning etc.... (3 Replies)
Discussion started by: Moxy
3 Replies

10. Linux

To find multiple strings count in a file

I need to find the line count of multiple strings in a particular file. The strings are as follows: bmgcc bmgccftp bsmsftp bulkftp cctuneftp crbtftp crmpos cso gujhr I am doing manual grep for each of the string to find the line count. The command i am using right now is: grep mark... (3 Replies)
Discussion started by: salaathi
3 Replies
Login or Register to Ask a Question