Main Content

Want to do a big batch edit? Nothing beats Perl!

Archive - Originally posted on "The Horse's Mouth" - 2010-03-01 16:34:18 - Graham Ellis

I still love Perl ...

Wanting to convert a file of lines like this:
  <img src=rp_153_track.jpg><br><br>
into lines like this:
  rp_153_track.jpg <img src=rp_153_track.jpg><br><br>

The code is as simple as:
  /=(.*?)>/;
  print " $_";


And it runs like this:
  perl -n pip images.html > labelledimages.html

See my results here




Wanting to copy all files in a subdirectory that were NOT present in any other subdirectory, and add in a "this is archived" header at the start of each of them:

opendir(DH,".");
@modules=grep(/[a-z]\d{3}/,readdir DH);
foreach $dir(@modules) {
  opendir(DH,$dir);
  next if ($dir eq "j610");
  while ($ent = readdir DH) {
    $found{$ent} .= "$dir ";
  }
}
opendir(DH,"X001");
@news = readdir(DH);
foreach $example(sort @news) {
  next if ($example =~ /^\./);
  if (! $found{$example}) {
    open (FH,"X001/$example");
    read (FH,$buffer,-s "X001/$example");
    close FH;
    open (FH,">j610/$example");
    print FH ("#%% $example\n");
    print FH ("--- ARCHIVED EXAMPLE (may not be current) ---\n");
    print FH ("--- See notes at http://www.wellho.net/mouth/2654.html ---\n\n");
    print FH ("$buffer\n");
    close FH;
    }
  }


Yes ... I know it's hard to follow, unstructured, unOO ... but it's PRACTICAL and Perl is nothing if not the practical extraction and reporting language!


Illustration - delegates on a Perl Training Course at our training centre.