Issue
Am unable to get the exact output for a XML
<String="12345">
<Bin>
<Value1 />
<Value2 />
</Bin>
</String>
Here need to find <String=12345>
, then insert a line after <Bin>
something similar to below
<String="12345">
<Bin>
<ValueXxxx />
<Value1 />
<Value2 />
</Bin>
</String>
Thanks in advance for ur help.
Solution
Although I don't intend to encourage people to parse or edit structured data
as xml
, the following would work for this specific case:
nl=$'\n'
nl="\\$nl"
sed '
:l
N
$!b l
s#\(<String="12345">\n[^\n]*\n\)#\1<ValueXxxx />'"$nl"'#
' input.xml
which yields:
<String="12345">
<Bin>
<ValueXxxx />
<Value1 />
<Value2 />
</Bin>
</String>
- At first it slurps all lines in pattern space to enable matching with multiple lines.
- the regex
<String="12345">\n[^\n]*\n\
matches with the specified line followed by an arbitrary line. - Then the
s
command inserts the string<ValueXxxx />
and a newline just after the matched lines.
Answered By - tshiono