Replace one regex, then replace second regex in all lines not matching first
You can use the t
command without a label to start next cycle on successful substitution
$ cat ip.txt
a foo 123
xyz
fore
1foo
$ sed -e 's/foo/bar/' -e t -e 's/$/baz/' ip.txt
a bar 123
xyzbaz
forebaz
1bar
From manual:
t label (test)
Branch to label only if there has been a successful substitution since the last input line was read or conditional branch was taken. The label may be omitted, in which case the next cycle is started.
A more robust way with awk
script:
awk '{ if (/foo/) gsub(/foo/, "bar"); else $0 = $0 "baz" }1' file
Or even shorter:
awk '{ if (!gsub(/foo/, "bar")) $0 = $0 "baz"; }1' file
Try this:
sed -e '/foo/!s/$/baz/g' -e s/foo/bar/g