Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple pattrens for grep 1

Status
Not open for further replies.

Calator

Programmer
Feb 12, 2001
262
AU
Hi,
I need to list more than one patterns on a grep command;
man says to separate the patterns with the newline character. I have tried:

grep '$1'\n'$2' $my_file

but it does not work
Any ideas? Thanks
 
grep -e "$1" -e "$2" $my_file

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks guys, both are working ok, but I tend to use PHV's solution.
As not always $2 has a value, I have coded the following which does not work (grep does not return the matches). Any idea why?
if [ -n "$1" ]; then
tg1="-e \"$1\""
fi;
if [ -n "$2" ]; then
tg2=" -e \"$2\""
fi;
my_string=${tg1}${tg2}
echo $my_string #displays ok as expected eg -e "error" -e "has aborted"
grep ${my_string} $my_file
 
Try this:
eval grep ${my_string} $my_file

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top