Wednesday, February 10, 2010

Simple Filters in Perl

The other day I needed to pull some XML out of a log file. Some of the XML is in human readable format, spanning multiple lines. I changed my logging to spit out a tag before and after the XML to make it easy to mechanically separate the XML from the rest of the log.1 The tags I used were "--- XML Request" or "--- XML Response" at the beginning of the line before the XML and "--- End XML" at the beginning of the line after the end of the XML. The Perl I came up with to filter these logs is:

#!/usr/bin/perl -w

$in_xml = 0;

while (<>&) {
  if ($in_xml) {
    print $_;
    if ($_ =~ /^--- End/) {
      $in_xml = 0;
    }
  } else {
    if ($_ =~ /^--- XML/) {
      print $_;
      $in_xml = 1;
    }
  }
}

I then remembered that I didn't have to specify the =~ or $_, Perl being able to assume that for you, my revised version is:

#!/usr/bin/perl -w

$in_xml = 0;

while (<>) {
  if ($in_xml) {
    print $_;
    if (/^--- End/) {
      $in_xml = 0;
    }
  } else {
    if (/^--- XML/) {
      print $_;
      $in_xml = 1;
    }
  }
}

1 This is entirely unlike mechanically separated chicken.

No comments:

Post a Comment