Home > database >  Using sed to replace a part of multine text and returning non 0 code when not finding a replacement
Using sed to replace a part of multine text and returning non 0 code when not finding a replacement

Time:08-14

im trying to switch the no part from the below text to yes using this sed command but doesnt seem to figure it out. Any help would be greatly appreciated!

command: sed -E "s@<wodle name="docker-listener">\n\s <disabled>(no)<\/disabled>@yes@g" /etc/test.txt

text in test.txt

<wodle name="docker-listener">
    <disabled>no</disabled>
</wodle>

<wodle name="example">
    <disabled>no</disabled>
</wodle>

UPDATE: Im in a better shape now with the below command, but the problem is that even if sed does not find a match im still get a return code of 0 and not the defined 100. Any clues how to fix this?

sed -E "N;!{q100};s@<wodle name=\"docker-listener\">\n\s <disabled>no@<wodle name=\"docker-listener\">\n <disabled>yes@g"

CodePudding user response:

sed  'H;1h;$!d;x;{/(<wodle name="docker-listener">\n[ ]*<disabled>)no(<\/disabled>)/!q100; s//\1yes\2/}' -E i; echo $?

0

sed  'H;1h;$!d;x;{/(<wodle name="docker-listener">\n[ ]*<disabled>)yes(<\/disabled>)/!q100; s//\1yes\2/}' -E i; echo $?

100

Since you insisted on matching \n and [[:space:]]; Storing entire file in buffer is only solution to match \n.

H;1h;$!d;x --> Store entire file in pattern buffer

/<wodle name="docker-listener">\n[ ]*<disabled>yes<\/disabled>/ --> Match for pattern

!{q100} --> sets exit code 100 if pattern not found else 0

s/// ---> will do actual replace

  • Related