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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if grep -- don't want it to print the matched string

Status
Not open for further replies.
Feb 14, 2002
88
JP
Greets -- I'm sure this is something incredibly silly and easy, that I'm just not getting. Let's say I've got this:

if grep "nimrod" mylog.log
then
print "there's a nimrod in your log!"
fi

And w/ a log file like
Bob is kewl
Jane is an idiot
You are a nimrod

the out put would be
$>nimrod.ksh
You are a nimrod
there's a nimrod in your log!

I want to get rid of the string that's matched in the log file, and only print out the string in the print statement when a match is found. Easy?

Thanks
 
Well, I guess I could output to a file, and do another grep w/ the -v option. Anything that might save that step?
 
Hi,
the status is set regardless of whether the output is displayed. Just put the output from the GREP to /dev/null which is the bit bucket. then check that status $?.

however I can't remember if GREP returns 0 when it works so you might have to negate the status.


grep "nimrod" mylog.log > /dev/null
if [ ! $? ]
then
print "there's a nimrod in your log!"
fi


 
You can use the -q option of grep.
With this option grep do not write anything to stdout and exit with zero status if an input line is selected.

grep -q "nimrod" mylog.log && print "there's a nimrod in your log!"

Jean Pierre.
 
For this type of operation would write as follows

GREPCOUNT=`grep -c "nimrod" mylog.log`
if [[ $GREPCOUNT > 0 ]]
then
print "there's a nimrod in your log!"
fi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top