0

I have a quit large XML file that I am trying to modify only certain lines of the file. The lines I want to modify will all contain in them. On those lines, I want to have only the text between 2 string (in this case those strings are / and ). So lets says I have this in a text file:

<title>xxxxxxxxx / xxx</title>
<sys>yyyyyyyyy</sys>
<name>test / extra text</name>
<date>zzzzzzzzz</date>

I want to modify it so I get the following:

<title>xxxxxxxxx / xxx</title>
<sys>yyyyyyyyy</sys>
<name>test</name>
<date>zzzzzzzzz</date>

Can this be accomplished with awk or sed?

2 Answers2

1

Use an XML parser to parse XML data.

# get the current value:
current=$(xmlstarlet sel --template --value-of "//name" file.xml)
# update the value
xmlstarlet ed --inplace --update "//name" --value "${current% /*}" file.xml
glenn jackman
  • 4,630
  • 1
  • 17
  • 20
0

This should work:

sed 's:<name>test / extra text</name>:<name>test</name>:' file.xml

Or for an in-place substitution, add -i:

sed -i 's:<name>test / extra text</name>:<name>test</name>:' file.xml

parkamark
  • 1,128
  • 7
  • 11