I want to replace specific xml node value using sed
or awk
. I can't use specialized packages for parsing xml like xmlstarlet
, xmllint
etc. I have to use sed
or awk
, just "basic" shell.
I have many big xml files. In that file I want to target and replace two tags values: example:
<desc:partNumber>>2</desc:partNumber>
<desc:dateIssued>>1870</desc:dateIssued>
Problem is, there are hundreds tags with these names. But these two tags have parent tag that is unique within whole xml file:
<desc:desc ID="DESC_VOLUME_0001">
Another problem is that location or line numbers of tags <desc:partNumber>
and <desc:dateIssued>
which are inside parent <desc:desc ID="DESC_VOLUME_0001">
are different in every file.
I think the solution would be:
- Target and extract parent
<desc:desc ID="DESC_VOLUME_0001">
and its children to variable - Iterate children and get location(line number) of
<desc:partNumber>
and<desc:dateIssued>
and save to variable - Pass the line number to
sed
command and replace current value of that tag with new value(new value will be read from .csv file)
I tried create this sed
command, you can see I used 'n
' to move over lines, but this needs to be variable.
sed -i '/desc:desc ID="DESC_VOLUME_0001"/{n;n;n;n;n;n;n;n;n;s/'"${OLD_DATE_ISSUED}"'/'"${NEW_DATE_ISSUED}"'/}'
Parent node with children:
<desc:desc ID="DESC_VOLUME_0001">
<desc:physicalDescription>
<desc:note>text</desc:note>
</desc:physicalDescription>
<desc:titleInfo>
<desc:partNumber>2</desc:partNumber>
</desc:titleInfo>
<desc:originInfo>
<desc:dateIssued>1870</desc:dateIssued>
</desc:originInfo>
<desc:identifier type="uuid">81e32d30-6388-11e6-8336-005056827e52</desc:identifier>
</desc:desc>
Can anybody help how to achieve this?