0

I have a XML file with

<RangeType>3</RangeType>
<RangeAmount>1</RangeAmount>
<FromDate>2016/8/12 00:00:00</FromDate>
<ToDate>2016/8/12</ToDate>
<SyncOnSelect>0</SyncOnSelect>
<RunEvery>0</RunEvery>

I want to replace Fromdate content (maybe anydate) to 2016/09/21, I plan to do this everyday, to change it to today

<RangeType>3</RangeType>
<RangeAmount>1</RangeAmount>
<FromDate>2016/09/21</FromDate>
<ToDate>2016/8/12</ToDate>
<SyncOnSelect>0</SyncOnSelect>
<RunEvery>0</RunEvery>

I tried

sed 's/<FromDate>*<\/FromDate>/<FromDate>2016\/09\/21<\/FromDate>/g'

How do I do this?

lwhuang
  • 45
  • 3
  • replace content to 2016/09/21 – lwhuang Oct 16 '16 at 03:00
  • 1
    This is not HTML, this is XML. Regardless, use an XML parser. – rockerest Oct 16 '16 at 03:04
  • just a typo in command you tried: `*` should be `.*` – Sundeep Oct 16 '16 at 04:01
  • It works, Thank you very much Sundeep – lwhuang Oct 16 '16 at 04:48
  • @lwhuang, only for a very poor value of "works". `sed` can't understand XML syntax: For instance, your document would still be completely valid XML (with exactly the same meaning to any compliant parser) if you had a newline or a space between ``, or no newlines at all anywhere in your file, but either of those changes would mess up your `sed`-based approach. – Charles Duffy Oct 16 '16 at 04:56
  • @rockerest, ...we have closer duplicates than that one -- it doesn't have *anything* to do with editing, after all. Thus, in this context, it's just an admonishment without any example of *how* to do things better, whereas there are clear Q&A entries showing how to use XSLTProc, XMLStarlet, Python-interpreter XML libraries, &c. from bash to correctly edit XML files from shell scripts. – Charles Duffy Oct 16 '16 at 04:58

1 Answers1

0

Use a real XML parser, such as XMLStarlet:

newValue=$(date '+%Y/%m/%d 00:00:00')
xmlstarlet ed -u '//FromDate' -v "$newValue" \
   <in.xml >out.xml
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441