algebaric operations on a RegEx?

William O'Higgins Witteman william.ohiggins-H217xnMUJC0sA/PxXw9srA at public.gmane.org
Sat Apr 4 18:28:46 UTC 2009


On Sat, Apr 04, 2009 at 01:18:05PM -0400, matt.price-H217xnMUJC0sA/PxXw9srA at public.gmane.org wrote:
> I want to quickly play with some numeric values in an xml file (xbmc's  
> Fontxml files, of which there are often a number in any given skin).   
> what I need to do is find lines like:
> <size>13</size> and augment or decrement the values by a set number.  so 
> i'd like a command addsize, like this:
>
> addsize 4 font.xml
>
> that would change  the above to <size>17</size>
>
> someone out there actually understand how to use perl, and willing to  
> show me how to do this?  thanks much,

My PerlFu is very rusty, but if you were to ask this question on
perlmonks.org, you'd get an answer pretty quickly.

If you are not wedded to Perl, there are other choices.  In Python, it
might look like this:

input file:

<xmlsample>
  <size>13</size>
</xmlsample>

Python code:

#!/usr/bin/python

import xml.etree.ElementTree as et

xmlobject = et.parse("in.xml")

for size in xmlobject.getiterator("size"):
  size.text = str(int(size.text) + 4)

outfile = open("out.xml","w")
xmlobject.write(outfile)

output file:

<xmlsample>
  <size>17</size>
</xmlsample>

I hope that helps!
-- 

yours,

William

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://gtalug.org/pipermail/legacy/attachments/20090404/ae419a29/attachment.sig>


More information about the Legacy mailing list