I need to insert lines into an xml file :
Insert
<one>
</one>
into
<tags>
</tags>
To obtain
<tags>
<one>
</one>
</tags>
I tried this:
sed "s#\\(< /tags>\\)#${multiline_string}\1#"
but it loses the indentation.
|
As Mat said, indentation (and whitespace in general) is not important in XML files. This:
Is exactly equivalent to:
But this will work while preserving indentation:
...but if you're working with XML, you might want to think about using a higher-level language that can actually parse XML and manipulate the tree programatically. |
|||
|
|
|
The following is a mod to the code you show in the question. Note that
The trailing |
||||
|
sedscript wasn't far off the mark. This works:sed "s|\(</tags>\)|${multiline_string//$'\n'/\n}\1|"... but larsks' append method may be more obvious. – Peter.O Feb 24 '12 at 17:04