Showing posts with label perl. Show all posts
Showing posts with label perl. Show all posts

Monday, September 21, 2015

Flatten and XML file (perl)

This perl one-liner is useful in case you need to "flatten" and XML file.



PERL_CONVERT="use XML::Simple; print XMLout(XMLin('-', KeepRoot => 1), AttrIndent => 1, KeepRoot => 1, XMLDecl => '');"

To be used like this :

/usr/bin/perl -e "$PERL_CONVERT" > $TMPFILE


Whatever input you have (in XML), the goal is to have a "comparable" ouptut that will respect the XML structure :
FROM:


...

...

 TO:


attr1="value1"
attr2="value2"
...
attr3="value3"
>

...




...


Specifically, this is really useful if you need to merge 2 different versions of  your XML file, like for example in SVN.

Tuesday, June 2, 2015

Replace all occurences of a string in a bunch of files by another string (in place)


  • Find all files on containing STRING_AAAA and replace it by STRING_BBBB in place (directly in the file).
find . -type f -exec grep -l STRING_AAAA {} \; -exec perl -pi -e 's!STRING_AAAA!STRING_BBBB!g' {} \;
Pre-tests :
  • Find all files on containing STRING_AAAA.
find . -type f -exec grep -l STRING_AAAA {} \;

Tuesday, May 25, 2010

Perl One-liner

Un des atouts de perl est la facilité avec laquelle il est possible d'écrire des scripts très compacts.

Un des défauts de perl est que rapidement quand le script devient un peu long.

Un bon compromis : faire des scripts très courts :-)


La page suivante présente un certain nombre d'exemple d'utilisation de perl sans passer par un script indépendant dans un fichier, et en utilisant plutôt une simple ligne ("One Liner") de code directement sur la ligne de commande :

http://sial.org/howto/perl/one-liner/