Operating System - HP-UX
1848183 Members
5805 Online
104022 Solutions
New Discussion

Re: search multiple pattern in a file

 
SOLVED
Go to solution
Anand_30
Regular Advisor

search multiple pattern in a file

Hi,

I have to search a file for the lines which contains both "ERROR" and "Records". Can anyone please help me to do this. I know "grep -e" option works for multiple patterns but It returns the lines which contains the mentioned pattern. But my requiremnet is that I need only the lines which contains both the above mentioned patterns.

Thanks,
Andy
8 REPLIES 8
Sundar_7
Honored Contributor
Solution

Re: search multiple pattern in a file

hmm I dont know if there is an "and" in the extended regular expression.

If all you want is to get the job done and could care less about elegance then try this :-)

# grep "ERROR" inputfile | grep "Records"

Learn What to do ,How to do and more importantly When to do ?
Rodney Hills
Honored Contributor

Re: search multiple pattern in a file

Usually I would do the following-

grep -e "ERROR.*Records" -e "Records.*ERROR" file

HTH

-- Rod Hills
There be dragons...
Geoff Wild
Honored Contributor

Re: search multiple pattern in a file

use 2 greps


grep "ERROR" | grep "Records"


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Jeff_Traigle
Honored Contributor

Re: search multiple pattern in a file

There's probably some fancy regexp stuff you can do with sed or awk or perl, but the following is a pretty simple solution:

grep ERROR filename | grep Records

or

grep Records filename | grep ERROR
--
Jeff Traigle
Govind_3
Regular Advisor

Re: search multiple pattern in a file

You can use egrep! saves some typing ;)
Cheers!
Govind_3
Regular Advisor

Re: search multiple pattern in a file

Sorry I forgot to add. But I agree with others using two greps would be the simplest solution!
Muthukumar_5
Honored Contributor

Re: search multiple pattern in a file

We can do this with sed / awk as,

sed -n '/ERROR/p;/Records/p' | uniq -d

awk '/ERROR/;/Records/ { print $0 }' | uniq -d
Easy to suggest when don't know about the problem!
Julián Esteban Aimar
Occasional Advisor

Re: search multiple pattern in a file

Use:
grep ERROR | grep Records