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.