Quick Question on sed command in shell script

Hello,
I have the following line in one of my shell scripts. It works fine when the search string($SERACH_STR) exists in the logfile($ALERTLOG) but if the search string does not exist this line errors out at run time. Is there a way to make this line return 0 if it is not able to find the search string in the log file?

START_LINE=`grep -n "$SEARCH_STR" $ALERTLOG | head -1 | sed 's/:confused: /' | awk '{print $1}'`

Thanks.

Or is there a way to trap the error generated by this command when it fails to find the search string?

Thanks

Near as I can tell, awk does support if/else, although I must confess to not having tried this before... I'll look into it and see what I can see...

  • Avron

Something like:

awk -v s=$SEARCH_STR '$0~s{exit(1)}END{exit}' $ALERTLOG
START_LINE=$?

Regards

Best that I could come up with is to assign `grep -n "$SEARCH_STR" $ALERTLOG | head -1` to a variable. If $variable, then proceed to the sed.

Since my arrival here at unix.com, I've constantly been amazed by the knowledge of people like Franklin52, era, jim mcnamara, et al.