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'
defined-or (Score:1)
Otherwise, strings like "0" and "0.0" will not be printed.
Re:defined-or (Score:2)
Re:defined-or (Score:1)
Re:defined-or (Score:2)
Before I knew Perl I would have ... (Score:1)
$ for $file in '*.html'
> do
> grep '' $file | head -n 3 | tail -1
> done | sort -u | sed -e 'whatever'
Or used awk