Using grep or something similar to invert search. | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Using grep or something similar to invert search.

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-25-2013
matcam79 matcam79 is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 27 March 2013, 12:10 PM EDT
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
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:


Code:
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
Sponsored Links
    #2  
Old 03-25-2013
hanson44 hanson44 is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 12 May 2013, 11:33 PM EDT
Posts: 858
Thanks: 18
Thanked 180 Times in 177 Posts
You can make grep -v work with multiple strings. For example:

Code:
$ 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:

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

Then, do the grep:

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

You can add in (or not) the $NEWDATE logic depending on your purposes.
The Following User Says Thank You to hanson44 For This Useful Post:
matcam79 (03-27-2013)
Sponsored Links
    #3  
Old 03-25-2013
matcam79 matcam79 is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 27 March 2013, 12:10 PM EDT
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
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.


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

Matthew
    #4  
Old 03-25-2013
PikK45 PikK45 is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 10 July 2014, 12:57 PM EDT
Location: Chennai
Posts: 567
Thanks: 43
Thanked 74 Times in 72 Posts
in that case egrep should help.


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

The Following User Says Thank You to PikK45 For This Useful Post:
matcam79 (03-27-2013)
Sponsored Links
    #5  
Old 03-25-2013
matcam79 matcam79 is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 27 March 2013, 12:10 PM EDT
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #6  
Old 03-25-2013
hanson44 hanson44 is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 12 May 2013, 11:33 PM EDT
Posts: 858
Thanks: 18
Thanked 180 Times in 177 Posts
Great. Yes, the -f option is pretty basic, so I'm glad you found a grep that supports it.
The Following User Says Thank You to hanson44 For This Useful Post:
matcam79 (03-27-2013)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk to search similar strings and arrange in a specified pattern prashu_g Shell Programming and Scripting 9 02-18-2012 10:52 AM
awk to search similar strings and add their values prashu_g Shell Programming and Scripting 3 01-12-2012 08:05 AM
How to invert order in QuickSort rafazz Programming 1 05-16-2010 03:55 PM
Help in grep function or similar using awk Lucky Ali Shell Programming and Scripting 1 10-20-2009 03:36 PM
Printing the invert of the last field of awk jehrome_rando Shell Programming and Scripting 2 09-13-2007 07:32 AM



All times are GMT -4. The time now is 11:44 PM.