The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
quick script C shell ajp7701 Shell Programming and Scripting 1 03-18-2008 04:31 PM
Quick help needed in the Shell Script namishtiwari Shell Programming and Scripting 1 02-01-2008 09:44 AM
need a quick basic shell script help eb222 Shell Programming and Scripting 6 11-22-2007 09:00 AM
Quick Question catbad UNIX for Dummies Questions & Answers 7 03-25-2003 07:01 PM
A Quick Question DPAI UNIX for Dummies Questions & Answers 4 04-01-2002 05:02 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-10-2008
luft luft is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 45
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/:/ /' | awk '{print $1}'`


Thanks.
  #2 (permalink)  
Old 09-10-2008
luft luft is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 45
Or is there a way to trap the error generated by this command when it fails to find the search string?

Thanks
  #3 (permalink)  
Old 09-10-2008
avronius avronius is offline VIP Member  
VIP Member
  
 

Join Date: Apr 2008
Location: Calgary
Posts: 305
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
  #4 (permalink)  
Old 09-10-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,305
Something like:
Code:
awk -v s=$SEARCH_STR '$0~s{exit(1)}END{exit}' $ALERTLOG
START_LINE=$?
Regards

Last edited by Franklin52; 09-10-2008 at 05:02 PM.. Reason: correcting code
  #5 (permalink)  
Old 09-10-2008
avronius avronius is offline VIP Member  
VIP Member
  
 

Join Date: Apr 2008
Location: Calgary
Posts: 305
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:38 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0