bigbadfisher
bigbadfisher

Reputation: 3

How can I find and increment datestrings in an XML file?

I have an XML file (actually XMLA) which will be used to dynamically drop and create partitions in MS Analysis Services on a rolling basis. My requirement is that I have to edit the file in unix as the file will be consumed and sent over to the AS server (ie I can't utilize SSIS). The XMLA is very predictable. I'll essentially have the following: the file will contain a delete section with a single PartitionID followed by a create section with a single PartitionID (I took out the noise).

<Batch>
  <Delete>
    <PartitionID>Cube_Part_201211</PartitionID>
  </Delete>
  <Create>
    <PartitionID>Cube_Part_201312</PartitionID>
  </Create>
</Batch>

The script should increment the first value to be *_201212 (ie plus one month) and the second value to be *_201401 (again plus one month). An alternative to make it easier (I hope) is that the value to be changed to can be relative to today's date instead of incrementing the existing value.

Upvotes: 0

Views: 117

Answers (1)

pobrelkey
pobrelkey

Reputation: 5973

One fast, but ugly, method...

perl -pe 's/(?<=<PartitionID>)(.*?)(\d{4})(?:(0[1-9]|1[01])|(12))(?=<\/PartitionID>)/sprintf("%s%04d%02d",$1,$2+($4?1:0),$4?1:($3+1))/eg' < input.xml > output.xml

Upvotes: 2

Related Questions