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
# 8  
Old 09-14-2017
Hi.

If you have the GNU utilities installed:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate extraction of multiple strings, egrep.

g=grep  # Linux
g=ggrep # Solaris, gnu-grep
# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C ggrep

FILE=${1-data1}
N=${FILE//[A-Za-z]/}
E=expected-output$N

pl " Input data file $FILE:"
cat $FILE

pl " Expected output:"
cat $E

pl " Results:"
# grep -Eo "String1|String2|String3" file.txt | sort | uniq -c
$g -Eo "String1|String2|String3|CRITICAL" $FILE |
tee f3 |
sort |
tee f2 |
uniq -c |
awk '{ print $2,$1}' |  # Swap fields
tee f1

pl " Verify results if possible:"
C=$HOME/bin/pass-fail
[ -f $C ] && $C f1 $E || ( pe; pe " Results cannot be verified." ) >&2

pl " Some details on $(which ggrep):"
man ggrep 2>/dev/null | head -7

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: SunOS, 5.11, i86pc
Distribution        : Solaris 11.3 X86
bash GNU bash 4.1.17
ggrep (GNU grep) 2.14

-----
 Input data file data1:
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 '/'

-----
 Expected output:
CRITICAL 3

-----
 Results:
CRITICAL 3

-----
 Verify results if possible:

-----
 Comparison of 1 created lines with 1 lines of desired results:
 Succeeded -- files (computed) f1 and (standard) expected-output1 have same content.

-----
 Some details on /usr/bin/ggrep:
GREP(1)                     General Commands Manual                    GREP(1)



NAME
       grep, egrep, fgrep - print lines matching a pattern

Best wishes ... cheers, drl
# 9  
Old 09-14-2017
Try this for an unlimited number of search strings compiled in the SRCH variable, separated by the pipe character:
Code:
awk 'match ($0, SRCH) { CNT[substr($0, RSTART, RLENGTH)]++} END {for (c in CNT) print c, CNT[c]}' SRCH="CRITICAL|ABORTING" file
ABORTING 1
CRITICAL 7

It doesn't work correctly if more than one search strings occur in a line; in that case, only the first one will be counted.
This User Gave Thanks to RudiC For This Post:
# 10  
Old 09-15-2017
Try this adaption for matching any number of search strings in a line:
Code:
awk '
                {P = 1
                 RSTART = RLENGTH = 0
                 while (match (substr ($0, P+=RSTART+RLENGTH), SRCH))   CNT[substr($0, P+RSTART-1, RLENGTH)]++
                }
END             {for (c in CNT) print c, CNT[c]
                }
' SRCH="CRITICAL|ABORTING|ABC|CDE|DEF|GHI|XYZ|ZYX" file

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