0

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.

  • Sounds like a job for [regex](http://perldoc.perl.org/perlre.html). – thomas.cloud Nov 11 '13 at 21:51
  • yup, its straightforward to find the row to operate on. Something as simple as: `sed 's@.*@NEW_DATE_STRING@g' my_file.xmla` I am struggling with the incrementing of the found value. – bigbadfisher Nov 11 '13 at 22:07
  • Take a look at the Perl module [Time::Piece](http://perldoc.perl.org/Time/Piece.html#Date-Calculations) for doing date calculations. – ThisSuitIsBlackNot Nov 11 '13 at 22:31

1 Answers1

2

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
pobrelkey
  • 5,853
  • 20
  • 29
  • Thanks for the response, but this is unaware of "date rollover" and unless I'm reading it wrong, its missing reassembling the line correctly in that its missing the $5 string part... right? The more I look at this, the more I want to go the route of date aware scripting. I suspect that will need to move away from a single -pe line and move into a full perl script. – bigbadfisher Nov 13 '13 at 16:36
  • @user2966875 "unaware of date 'rollover'"? This should handle incrementing December dates, if that's what you're asking (it uses the presence or absence of `$4` to detect December and handle it differently). And no, there's no `$5` group - note that because of the lookbehind/lookahead it only performs substitution on text inside a `` element. But yeah, if you're parsing XML and doing date math you should really use libraries for both those purposes - I mostly wrote this as a one-liner for fun. – pobrelkey Nov 13 '13 at 17:19
  • Thanks for the explanation. I'm still trying to wrap my head around regex syntax! – bigbadfisher Nov 13 '13 at 20:38