Quick Question on sed command in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Quick Question on sed command in shell script
# 1  
Old 09-10-2008
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  
Old 09-10-2008
Or is there a way to trap the error generated by this command when it fails to find the search string?

Thanks
# 3  
Old 09-10-2008
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  
Old 09-10-2008
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 06:02 PM.. Reason: correcting code
# 5  
Old 09-10-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Quick sed/awk question

Hi fellow linux-ers, I have a quick question for you. I have the following text, which I would like to modify: 10 121E(121) 16 Jan 34S 132E 24 Feb 42 176E(176) 18 Sep 21S 164E 25 May 15 171W(-171) 09 Jul How can I do the following 2 modifications using sed and/or awk? 1. in 1st column,... (1 Reply)
Discussion started by: lucshi09
1 Replies

2. Shell Programming and Scripting

Quick Sed Question

Just want to know why when I do the following in sed, the required is not extracted. echo "ab01cde234" | sed 's/*$//' result: ab01cde (Which is correct) echo "ab01cde234" |sed 's/.*\(*\)$/\1/' result: blank (was expecting 234) or echo "ab01cde234" |sed 's/.*\(\)*$/\1/' result: blank... (6 Replies)
Discussion started by: eo29
6 Replies

3. Shell Programming and Scripting

Quick question: calling c-shell script from bash

Hello, I have a quick reference question: I have a very long, but fairly straigtforward script written in c-shell. I was wondering if it is possible to call this script from bash (for ex. having a function in bash script which calls the c-shell script when necessary), and if so, are there any... (1 Reply)
Discussion started by: lapiduslost
1 Replies

4. Shell Programming and Scripting

Quick Help in shell script

Need to subtract date stored in variable from the current date, answer should come in days..and months. Suppose two variable having value like A=”Wed Jan 15 08:59:08 GMT 2014” B= `date` #Sun May 23 09:29:40 GMT 2010 SubtractAB= $A-$B #..? AddAB=$A+$B #... ? C=$B+9 # Sun May 23... (3 Replies)
Discussion started by: KuldeepSinghTCS
3 Replies

5. Shell Programming and Scripting

shell script/sed command help

First off I have read the man pages for sed and am still having trouble working on a script to remove portions of a log: My goal is to take a log file to be emailed, read the file and strip the portions away AFTER the line MIME-Version:1.0 and strip away until it to the line starting with... (4 Replies)
Discussion started by: murphybr
4 Replies

6. Shell Programming and Scripting

Need help using sed command in shell script?

Hello, i want to take the input from user and according to that variable's value search in file emp.lst. Here is what i came up with echo -e "Enter string to be searched :\c" read str sed -n '/\$str/p' emp.lst this is not working! any idea why?Thanks in advance! :) (4 Replies)
Discussion started by: salman4u
4 Replies

7. Shell Programming and Scripting

Sed command in shell script

I have a current code working(named subst1) having a user be able to type this line to substitute words using the sed command: subst1 old-pattern new-pattern filename Here is my shell script: #!/bin/bash # subst1 ARGS=3 E_BADARGS=65 if then echo "Usage: `basename $0`... (1 Reply)
Discussion started by: Todd88
1 Replies

8. Shell Programming and Scripting

quick script C shell

Cool. I played with scripts at home over the weekend. Come to find out not working on other shells. I have linux/bash at home, but now I'm trying on Solaris csh. How would I write the following script for Solaris C shell? ---------- #!/bin/bash NBR=231 for ((i = 0; i < $NBR; i++ )) do... (1 Reply)
Discussion started by: ajp7701
1 Replies

9. Shell Programming and Scripting

quick sed question

hey, Im just wondering is there away to get sed to read from a variable eg it doesn't seem to work, i really need to be able to recursively change the same data set... (2 Replies)
Discussion started by: vbm
2 Replies

10. Shell Programming and Scripting

a quick SED question

I have a line EXTDIR=`echo $i | sed 's/\-tar.gz//'` which looks for files ending in -tar.gz, i would like to increase the functionality so that it looks for .tar.gz files as well as -tar.gz. Do i put the - in square brackets with a dot ? like this EXTDIR=`echo $i | sed 's/\tar.gz//'` ... (1 Reply)
Discussion started by: hcclnoodles
1 Replies
Login or Register to Ask a Question