Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to search and replace some line from files, but when I run sed if that regex not match then sed does not return any status code, So instead of adding one more condition with grep, is there way in sed and Why sed does not return exit status ?

Problem :

enter image description here

share|improve this question
1  
That one got answered on StackOverflow, stackoverflow.com/questions/15965073/… –  Drav Sloan Aug 25 '13 at 14:55
    
@DravSloan - it's OK to replicate some of the answers here, go for it. –  slm Aug 25 '13 at 14:57
add comment

2 Answers

up vote 7 down vote accepted

sed does return an exit status:

$ echo "foo" | sed 's/xx/yy/'
foo
$ echo $?
0
$ sed 's/xx/yy/' foo.txt
sed: can't read foo.txt: No such file or directory
$ echo $?
2

Exit codes are about whether a program exited successfully or not, they have nothing to do with the internals of what sed is doing, just with whether or not the command managed to run.

That said, GNU sed does offer a way to do this:

   q [exit-code]
          Immediately  quit  the sed script without pro‐
          cessing any more input, except that  if  auto-
          print  is  not  disabled  the  current pattern
          space will be printed.  The exit code argument
          is a GNU extension.

For example (taken from here):

$  echo "foo"|sed '/foo/ s/f/b/;q'                                                                       
boo
$  echo $?
0
$ echo "trash"|sed  '/foo/{s/f/b/;q}; /foo/!{q100}'
trash
$  echo $?
100
share|improve this answer
add comment

First, all commands, including sed, return an exit status. You are looking for why I wouldn't be returning a failure. Which leads to the second point.

Programs like sed are editors, not predicates. They are there to change data flowing through the program. Error conditions are for conditions like invalid internal commands or a non-existent file on the command-line.

I'm not sure of what logic you want - you don't include any expected output - but this might be what you are looking for:

$ sed '/^test line 7/s//#test line 7/;/^#test line 7/s//& \nAppend New Line/' test.txt
test line 1
test line 2
test line 3
test line 4
test line 5
test line 6
#test line 7
Append New Line

There are other constructs which can handle this as well, for example the b (branch) command in sed.

If you want an actual return code if not found, then you might want to look at gawk and its exit statement.

share|improve this answer
    
still it's keep appending line if we run again and again.. –  Rahul Patil Aug 25 '13 at 15:12
    
I have solved using grep, but want to know in sed .. github.com/rahulinux/bash-functions-/blob/master/… –  Rahul Patil Aug 25 '13 at 15:13
    
You want to run the second statement only if the first one succeeds? You can add the GNU extension command, T, between the two: sed -i '/^test line 7/s//#&/;T;/#test line 7/&\nAppend New Line/' test.txt –  Arcege Aug 25 '13 at 15:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.