Single grep to multiple strings with separate output per string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Single grep to multiple strings with separate output per string
# 1  
Old 09-13-2017
Single grep to multiple strings with separate output per string

I need to grep multiple strings from a particular file.

I found the use of egrep "String1|String2|String3" file.txt | wc-l

Now what I'm really after is that I need to separate word count per each string found. I am trying to keep it to use the grep only 1 time.

Can you guys help ?

---------- Post updated at 03:17 PM ---------- Previous update was at 03:15 PM ----------

To explain myself better is that I'm after something similar:

Code:
String1 2 
String2 1
String3 4.....


Last edited by rbatte1; 09-13-2017 at 01:32 PM.. Reason: Added CODE and ICODE tags and correcting some spelling
# 2  
Old 09-13-2017
Quote:
Originally Posted by nms
I need to grep multiple strings from a particular file.
I found the use of egrep "String1|String2|String3" file.txt | wc-l
Now what I'm really after is that I need to separate word count per each string found. I am trying to keep it to use the grep only 1 time.
Can you guys help ?
---------- Post updated at 03:17 PM ---------- Previous update was at 03:15 PM ----------
To explain myself better is that I'm after something similar:
Code:
String1 2 
String2 1
String3 4.....

Hello nms,

Your complete requirement is not clear, so based on your explanation, could you please try following and let me know if this helps.
Code:
awk '/String1/{count1++;next} /String2/{count2++;next} /String3/{count3++;next} END{print "String1 ",count1 RS "String2 ",count2 RS "String3 ",count3}'  Input_file

Thanks,
R. Singh

Last edited by rbatte1; 09-13-2017 at 01:34 PM.. Reason: Retro-fitted the corrections made to the original post.
# 3  
Old 09-13-2017
How about (untested)
Code:
grep -Eo "String1|String2|String3" file.txt | sort | uniq -c

These 2 Users Gave Thanks to RudiC For This Post:
# 4  
Old 09-14-2017
I am running on Solaris 11, hence grep -o does not work.

What you provided me works but I'm looking for a command that works the same as 'grep -o' which works on Solaris.

Basically I have a log file and each line contains a timestamp. Therefore when performing uniq command, each line which contains that particular grep, is displayed separately.

To give you a better idea:

These are lines from the log file:
Code:
 1 Sep 14 11:00:01 ccsWalletExpiry: [ID 848595 user.crit] ccsWalletExpiry(28635) CRITICAL: ABORTING: Cannot connect to O                                                                     racle as '/'
   1 Sep 14 11:00:06  ccsPeriodicCharge: [ID 848595 user.crit] ccsPeriodicCharge(28632) CRITICAL: Error: failed to initiali                                                                     se database connection, cannot continue.
   1 Sep 14 11:10:00  ccsWalletExpiry: [ID 848595 user.crit] ccsWalletExpiry(12949) CRITICAL: ABORTING: Cannot connect to O                                                                     racle as '/'

I need to grep specifically for CRITICAL only and the output should be the string I'm trying to grep and the number of occurrences this string was matched

Code:
CRITICAL 3


Last edited by Scrutinizer; 09-14-2017 at 11:16 AM.. Reason: code tags
# 5  
Old 09-14-2017
Hello nms,

Could you please try following and let me know if this helps you.
Code:
awk '/CRITICAL/{count++} END{print "CRITICAL ",count}'   Input_file

on a Solaris/SunOS system, change awkto /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk. Let me know how it goes then.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 09-14-2017
Hi RavinderSingh13,

Yes that works.
The output is
CRITICAL 919.

However I would like to know if it's possible to include multiple strings.
# 7  
Old 09-14-2017
Quote:
Originally Posted by nms
Hi RavinderSingh13,
Yes that works.
The output is
CRITICAL 919.
However I would like to know if it's possible to include multiple strings.
Hello nms,

I believe I had given this answer in my POST#2 of this thread https://www.unix.com/303003374-post2.html, kindly have a look to it and let me know if you have any queries on same. Also you could hit THANKS button to thank anyone for a useful post at left corner of each post.

PS: In my post#2 I have given examples like STRING1, STRING2. Similarly you could put your actual strings on their place.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Output to file print as single line, not separate line

example of problem: when I echo "$e" >> /home/cogiz/file.txt result prints to file as:AA BB CC I need it to save to file as this:AA BB CC I know it's probably something really simple but any help would be greatly appreciated. Thank You. Cogiz (7 Replies)
Discussion started by: cogiz
7 Replies

2. Shell Programming and Scripting

Grep strings on multiple files and output to multiple files

Hi All, I want to use egrep on multiple files and the results should be output to multiple files. I am using the below code in my shell script(working in Ksh shell). However with this code I am not attaining the desired results. #!/bin/ksh ( a="/path/file1" b="path/file2" for file in... (4 Replies)
Discussion started by: am24
4 Replies

3. Shell Programming and Scripting

Find multiple strings and replace single string

Hi, following Perl code i used for finding multiple strings and replace with single string. code: #!/usr/bin/perl my @files = <*.txt>; foreach $fileName (@files) { print "$fileName\n"; my $searchStr = ',rdata\)' | ',,rdata\)' | ', ,rdata\)'; my $replaceStr =... (2 Replies)
Discussion started by: chettyravi
2 Replies

4. Shell Programming and Scripting

Multiple search strings replaced with single string

Hi, I need someone's help in writing correct perl code. I implemented following code for "multiple search strings replaced with single string". ========================================================= #!/usr/bin/perl my $searchStr = 'register_inst\.write_t\(' |... (2 Replies)
Discussion started by: chettyravi
2 Replies

5. 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

6. 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

7. UNIX for Dummies Questions & Answers

awk to match multiple regex and create separate output files

Howdy Folks, I have a list that looks like this: (file2.txt) AAA BBB CCC DDD and there are 24 of these short words. I am matching these patterns to another file with 755795 lines (file1.txt). I have this code for matching: awk -v f2=file2.txt ' BEGIN { while(... (2 Replies)
Discussion started by: heecha
2 Replies

8. 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

9. 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

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