Skip to content

Instantly share code, notes, and snippets.

@aterga
Created November 13, 2012 10:46
Show Gist options
  • Save aterga/4065160 to your computer and use it in GitHub Desktop.
Save aterga/4065160 to your computer and use it in GitHub Desktop.
A useful Perl subroutine for finding matched lines in a text file. Handy for extraction of required content from a huge log files.
#!/usr/bin/perl -w
use strict;
use warnings;
if ($#ARGV != 2)
{
print " Usage: trace_extract_mlt_region.pl [TRACE_FILENAME] [DUMP_FILENAME] [STORE_OPERATION_INDEX]\n";
exit;
}
sub find;
my $tracefile = $ARGV[0];
my $dumpfile = $ARGV[1];
my $st_operation = " $ARGV[2]. st8";
find($dumpfile, $st_operation)->[0] =~ /RegionID\d+/;
my $mlt_region = $&;
find($dumpfile, "mlt\.lock\.rw\.$mlt_region")->[0] =~ m/ \d+\. ld\d+/;
my $ld_operation = $&;
print @{find($tracefile, $st_operation, $ld_operation)};
sub find
{
if ($#_ < 1)
{
print " Error: function 'find' needs 2 or more args.\n";
print " First arg: filename, second arg: pattern, ...\n";
die;
}
open FILE, $_[0] or die $!;
splice(@_, 0, 1);
my @text = ();
foreach my $line (<FILE>)
{
foreach my $pattern (@_)
{
if ($line =~ m/$pattern/)
{
push(@text, $line);
}
}
}
close FILE;
return \@text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment