Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

i have this xml which i like to comment by string this is my xml ( part of it )

<subsystem
    xmlns="urn:jboss:domain:datasources:5.0">
    <datasources>
        <datasource jta="true" jndi-name="java:/comp/env/jdbc/app1" pool-name="app1" enabled="true"
                            use-ccm="false">
            <test1></test1>
            <test2></test2>
        </datasource>
         
            <datasource jta="true" jndi-name="java:/comp/env/jdbc/app2" pool-name="app2" enabled="true"
                            use-ccm="false"><test1></test1><test2></test2></datasource>
             
    </datasources>
</subsystem>

i tried this sed regexp but was able to capture only the first <datasource tag this is what i tried :

sed -E '/./{H;1h;$!d} ; x ; s/(<datasource.*app1*)/ <!--
1-->/gi'

based on this section 6.2

i like to capture the and put it in comment so it will look like this

<subsystem
    xmlns="urn:jboss:domain:datasources:5.0">
    <datasources>
<!--
        <datasource jta="true" jndi-name="java:/comp/env/jdbc/app1" pool-name="app1" enabled="true"
                            use-ccm="false">
            <test1></test1>
            <test2></test2>
        </datasource>
-->

         
            <datasource jta="true" jndi-name="java:/comp/env/jdbc/app2" pool-name="app2" enabled="true"
                            use-ccm="false"><test1></test1><test2></test2></datasource>
             
    </datasources>
</subsystem>

UPDATE
please dont close the question
i dont have xml tools on my host only basic linux tools

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
154 views
Welcome To Ask or Share your Answers For Others

1 Answer

i dont have xml tools on my host only basic linux tools

In the absence of XML parsing tools, you can use this basic sed command to comment out a block as you want. Please bear in mind that this will not be a robust solution as offered by tools like xmllint.

You may use this sed:

sed '/<datasource [^>]*app1/,/</datasource>/ {s/<datasource /<!--
	&/; s~</datasource>~&
	-->~;}' file.xml

<subsystem
    xmlns="urn:jboss:domain:datasources:5.0">
    <datasources>
    <!--
    <datasource jta="true" jndi-name="java:/comp/env/jdbc/app1" pool-name="app1" enabled="true"
                            use-ccm="false">
            <test1></test1>
            <test2></test2>
        </datasource>
    -->

            <datasource jta="true" jndi-name="java:/comp/env/jdbc/app2" pool-name="app2" enabled="true"
                            use-ccm="false"><test1></test1><test2></test2></datasource>

    </datasources>
</subsystem>

Explanation:

  • /<datasource [^>]*app1/: Match range start from <datasource tag that has text app1 in it somewhere
  • ,/</datasource>/: range end with closing </datasource> tag.
  • {: Start action block
    • s/<datasource /<!-- &/;: Prepend opening <datasource tag with a comment start line <!--
    • s~</datasource>~& -->~;: Append closing </datasource> with comment closing line -->
  • }: End action block

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...