Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

ziggy (25)

  (email not shown publicly)
AOL IM: ziggyatpanix (Add Buddy, Send Message)

Journal of ziggy (25)

Wednesday August 10, 2005
08:52 AM


[ #26219 ]

Here's a little script I whipped up the other day. I'm grepping through a zillion files, all of which have the same structure but different data. Therefore, an operation like

$ grep '<span class="interesting">' *.html

produces about a dozen "interesting" lines of output per file. One thing I wanted to do was grab the 3rd such value from each file, and then compare them. Thus was born nth:

#!/usr/bin/perl -w

use strict;

my %values;
my @keys;

my $n = shift(@ARGV) -1;

while (<>) {
    my ($key) = m/^(.*?):/;

    push(@keys, $key) unless exists $values{$key};
    push(@{$values{$key}}, $_);

print map {$_->[$n] or "\n"} @values{@keys};

Extracting the interesting bits of data is now as simple as:

$ grep '<span class="interesting">' *.html | nth 3 | sed -e 'whatever'

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.