How to comment out a string in xml file in shell

2019-08-31 09:33发布

问题:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE sailpoint PUBLIC 'sailpoint.dtd' 'sailpoint.dtd'>
<sailpoint>
<ImportAction name='include' value='custom/Application/CARD/CommonCardFiles/COF Imaging Applications Custom Object.xml'/>
<ImportAction name='include' value='custom/Configuration/COF - Singlenter code heree Entitlement Application Custom Object.xml'/>
<ImportAction name='include' value='custom/Rule/COF Related Application Library.xml'/>
<ImportAction name='include' value='custom/Form/COF LCM CBT Form.xml'/>
<ImportAction name='include' value='custom/Workflow/COF Identity Refresh Bypass.xml'/>
<ImportAction name='include' value='custom/Configuration/COF Unstable Roles Config.xml'/>
<ImportAction name='include' value='custom/Application/CARD/CommonCardFiles/COF Salesforce Common Correlation Rule.xml'/>
<ImportAction name='include' value='custom/Rule/COF Single SOD Violation Detection Rule.xml'/>
<ImportAction name='include' value='custom/Configuration/COF APPLICATION DEPENDENCY 
</sailpoint>*

On above mention code i am trying to comment out all the line with pattern custom/Application in it

sed command i use that is not working

sed '/<custom&Application>/s/\(.*\)/<--\1-->/' est.xml > new.xml

回答1:

This should work

sed -e '/custom\/Application/s/\(^.*$\)/<!--\1-->/' est.xml > new.xml



回答2:

You can match the complete lines with custom and Applicatioin in it. Use & for the matched string.

sed 's/.*custom.*Application.*/<!--&-->/' est.xml > new.xml


回答3:

XML data should be parsed with an XML parser.

You can delete those tags with xmlstarlet

xmlstarlet ed -d '//ImportAction[contains(@value,"custom/Application")]' file.xml

I'm not sure how to comment them.