How to remove comments from an XML file?
By Sarah Rodriguez •
Is there a command line tool that can remove comments from an XML file? Or do I need to write a small program that makes use of an XML parser to do this?
Update: I'm not interested in solutions that only handle a subset of all possible XML files.
For instance a regexp can't handle XML parsing.
2 Answers
I would do it in this way:
cat myfile.xml | sed '/<!--.*-->/d' | sed '/<!--/,/-->/d' > cleaned.xmlOr:
awk 'in_comment&&/-->/{sub(/([^-]|-[^-])*--+>/,"");in_comment=0} in_comment{next} {gsub(/<!--+([^-]|-[^-])*--+>/,""); in_comment=sub(/<!--+.*/,""); print}'Or:
xmlstarlet ed -d '//comment()' file.xml 4 To expand on the top answer. If you only want to delete the comment and not the entire line, you should probably use:
sed 's/<!--.*-->//'In my case, I had a minified XML file where the entire content was in a single line and since the previous solution would delete the entire line where the comment was located, it would completely clear out my file.
1