0

Please, I have this xml balise inside my test.xml file

<ingressAnnotations>nginx.ingress.kubernetes.io/server-snippet: |
  location @custom_503 {
    return 503 &quot;&lt;html&gt; &lt;head&gt; &lt;meta http-equiv=&apos;Content-Type&apos; content=&apos;text/html; charset=UTF-8&apos;&gt; &lt;style&gt;...&lt;/style&gt;&lt;/head&gt; &lt;body&gt;&lt;img src=&apos;https://www.jenkins.io/images/logos/jenkins-is-the-way/j
enkins-is-the-way.png&apos; width=&apos;200&apos; height=&apos;200&apos; style=&apos;display: block; margin-left: auto; margin-right: auto;&apos; alt=&apos;jenkins&apos;&gt;&lt;center&gt;&lt;h2&gt;Jenkins is sleeping, please go to jenkins.betclic.net and click your Maste
r link to wake him up. It will be available in a few minutes. This is the wait !!!&lt;/h2&gt;&lt;/center&gt;&lt;/body&gt;&lt;/html&gt;&quot;;
  }
  error_page 503 @custom_503;</ingressAnnotations>

I have to parse my file and remove the content. Something like that:

<ingressAnnotations></ingressAnnotations>

How can I do that by sed please ? I'm trying this:

sed -i 's/<ingressAnnotations>*<\/ingressAnnotations>/<ingressAnnotations><\/ingressAnnotations>/g' test.xml

But it does not work !

2
  • "It does not work" - What did you expect to happen? What actually happens? Did you get any error messages? Don't use -i while you're testing. Dec 23, 2021 at 10:19
  • 1
    please do not try to parse XML with regular expression tools. XML is not a regular language, and cannot reliably be parsed with them. Dec 23, 2021 at 12:43

2 Answers 2

4

You can use an XML parser to parse and edit your file. This command matches the <ingressAnnonations/> tag anywhere and everywhere in your document and removes all its content:

xmlstarlet edit --update '//ingressAnnotations' --value '' test.xml

Output

<?xml version="1.0"?>
<ingressAnnotations/>

Include the --inplace parameter (i.e. xmlstarlet edit --inplace --update …) to edit the file in place once you are sure the transformation works as expected

0

Clumsy, but works:

$ sed -n '/<ingressAnnotations>/{p; :a; N; /<\/ingressAnnotations>/!ba; s/.*\n//}; p' \
FILENAME | sed -e 's/>.*/>/g' -e 's/.*<\//<\//g' 

The above gives:

<ingressAnnotations>
</ingressAnnotations>

EDITED.

To remove newline between the patterns, add | sed -e ':a;N;$!ba;s/\n//g' pipe, so:

$ sed -n '/<ingressAnnotations>/{p; :a; N; /<\/ingressAnnotations>/!ba; s/.*\n//}; p' \
FILENAME \
| sed -e 's/>.*/>/g' -e 's/.*<\//<\//g' \
| sed -e :a;N;$!ba;s/\n//g'
3
  • No, it will work with the above pattern, so if there are new lines involved.
    – Bart
    Dec 23, 2021 at 10:27
  • @roaima, right about -i, that would have to be redirected to a file
    – Bart
    Dec 23, 2021 at 10:28
  • 2
    printf "<root>\n<ingressAnnotations>nginx.ingress.kubernetes.io/server-snippet: |\n location @custom_503 { }\n error_page 503 @custom_503;</ingressAnnotations>\n<ingressAnnotations>boo</ingressAnnotations>\n<ingressAnnotations>\nmultiline\ncomments\n</ingressAnnotations>\n</root>\n" generates valid XML that fails your transformations. I don't know how plausible it is in the OP's context though. The point is that sed on XML is fragile Dec 23, 2021 at 10:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .