Using grep or something similar to invert search.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using grep or something similar to invert search.
# 1  
Old 03-25-2013
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
# 2  
Old 03-25-2013
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.
This User Gave Thanks to hanson44 For This Post:
# 3  
Old 03-25-2013
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
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

This User Gave Thanks to PikK45 For This Post:
# 5  
Old 03-25-2013
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
# 6  
Old 03-25-2013
Great. Yes, the -f option is pretty basic, so I'm glad you found a grep that supports it.
This User Gave Thanks to hanson44 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use a grep search to search for a specific string within multiple directories?

Lets say I have a massive directory which is filled with other directories all filled with different c++ scripts and I want a listing of all the scripts that contain the string: "this string". Is there a way to use a grep search for that? I tried: grep -lr "this string" * but I do not... (3 Replies)
Discussion started by: Circuits
3 Replies

2. Shell Programming and Scripting

To search for a particular tag in xml and collate all similar tag values and display them count

I want to basically do the below thing. Suppose there is a tag called object1. I want to display an output for all similar tag values under heading of Object 1 and the count of the xmls. Please help File: <xml><object1>house</object1><object2>child</object2>... (9 Replies)
Discussion started by: srkmish
9 Replies

3. Shell Programming and Scripting

awk to search similar strings and arrange in a specified pattern

Hi, I'm running a DB query which returns names of people and writes it in a text file as shown below: Carey, Jim; Cena, John Cena, John Sen, Tim; Burt, Terrence Lock, Jessey; Carey, Jim Norris, Chuck; Lee, Bruce Rock, Dwayne; Lee, Bruce I want to use awk and get all the names... (9 Replies)
Discussion started by: prashu_g
9 Replies

4. Shell Programming and Scripting

awk to search similar strings and add their values

Hi, I have a text file with the following content: monday,20 tuesday,10 wednesday,29 monday,10 friday,12 wednesday,14 monday,15 thursday,34 i want the following output: monday,45 tuesday,10 wednesday,43 friday,12 (3 Replies)
Discussion started by: prashu_g
3 Replies

5. Programming

How to invert order in QuickSort

Hello! Iam trying to reverse my order of quicksort in C from asc order (1-99) that is the original way of the algoritm, to descending (99-1). I've tried so many ways that iam a kind of lost now!:s The original quicksort is: void swap(int* a, int* B) { int tmp; tmp = *a; *a = *b; ... (1 Reply)
Discussion started by: rafazz
1 Replies

6. Shell Programming and Scripting

Invert Matrix of Data - Perl

I have columnar data in arrays perl, Example - @a = (1,2,3); @array1 = (A,B,C); @array2 = (D,E,F); @array3 = (I,R,T); I want the data to be formatted and printed as 1 A D I 2 B E F 3 C F T and so on... (8 Replies)
Discussion started by: dinjo_jo
8 Replies

7. Shell Programming and Scripting

Help in grep function or similar using awk

I have a list of id; for example: file 1 dfghd dfghe dfgey dfgeu I have another data file that contain this ids as headers; for ex. file2 >dfghd gfdgfddl;klfkld;ld;lgl;dld'l'dv >dfghe gkwhjhsgdjdjdjhjddj >dfgey jdkjfhdjhfdkjhfdkhkdk I wanted to compare file 1 and file 2... (1 Reply)
Discussion started by: Lucky Ali
1 Replies

8. UNIX for Dummies Questions & Answers

what is the similar command for "grep -v" in nawk?

Can any one please say about the below, using, grep -v "name" file.txt the result of above command will be it will print all the lines except the line which having the word "name" similarly, nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r;print;c=a}b{r=$0}' b=0 a=1 s="name" file.txt ... (7 Replies)
Discussion started by: prsam
7 Replies

9. Shell Programming and Scripting

Printing the invert of the last field of awk

in csh set x = "/home/usr/dir1/file1" if i do: echo $x | awk -F\/ '{print $NF}' will result to: "file1" how do i invert the output to: "/home/usr/dir1" :confused: (2 Replies)
Discussion started by: jehrome_rando
2 Replies
Login or Register to Ask a Question