Using grep or something similar to invert search.

Hi guys,

I have a script in which it simply do a grep of strings for the var/adm/messages file:

NEWDATE=`TZ=GMT+1 date +%b" "%d" "%H`

getalarm1=`grep "sent to primary BE" /var/adm/messages* | grep "$NEWDATE" | wc -l`
getalarm2=`grep "CC-Request-Type 3 received in state Idle" /var/adm/messages* | grep "$NEWDATE" | wc -l`
getalarm3=`grep "Unknown session-id. Origin-Host" /var/adm/messages* | grep "$NEWDATE" | wc -l`

My query is this. How can i get a word count of the rest of the messages excluding these searches. I could think of using grep -v command but it doesn't seem to work using multiple strings.

Can you please help me out?

Thanks,

Matthew

You can make grep -v work with multiple strings. For example:

$ cat patterns.txt
xxx
yyy

$ cat input.txt
jjj
ggg
xxx
yyy

$ grep -v -f patterns.txt input.txt
jjj
ggg

Make your pattern file:

$ cat patterns.txt
sent to primary BE
CC-Request-Type 3 received in state Idle
Unknown session-id. Origin-Host

Then, do the grep:

grep -v -f patterns.txt /var/adm/messages* | wc

You can add in (or not) the $NEWDATE logic depending on your purposes.

1 Like

Hi hanson44,

Thanks for your reply. that seem to be a good option but unfortunately it seems that an old version of grep is installed.

# grep -v -f revertsearch.txt /var/adm/messages | wc
grep: illegal option -- f
Usage: grep -hblcnsviw pattern file . . .
       0       0       0

Matthew

in that case egrep should help.

egrep -v "sent to primary BE|CC-Request-Type 3 received in state Idle|Unknown session-id. Origin-Host" /var/adm/messages* | wc -l
1 Like

Hi,

it seems you need to actually use /usr/xpg4/bin/grep to obtain patter for file, not the default /usr/bin/grep.

Hanson44, using the -f together with the -v option then worked. Many thanks for your help!

Matthew

Great. Yes, the -f option is pretty basic, so I'm glad you found a grep that supports it.

1 Like