Skip to content

Instantly share code, notes, and snippets.

@M0ses
Last active August 29, 2015 14:19
Show Gist options
  • Save M0ses/fe828c3a8548c6d4cd11 to your computer and use it in GitHub Desktop.
Save M0ses/fe828c3a8548c6d4cd11 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;
use Data::Dumper;
use Getopt::Long;
my $spattern;
my $epattern;
my $estart=0;
my $eend=0;
my $infile="/dev/stdin";
my $result_set = 0;
if ( ! GetOptions ("start-pattern=s" => \$spattern,
"end-pattern=s" => \$epattern,
"exclude-start" => \$estart,
"exclude-end" => \$eend,
"infile" => \$infile,
"result" => \$result_set
)
) {
print_usage();
exit 1;
}
open(IN,$infile);
my $track=0;
my $result=[];
my $counter=-1;
while (my $line = <IN>) {
chomp($line);
my $push_act_line = 0;
my $found_first=0;
if ( $track ) {
$push_act_line = 1;
}
if ( $line =~ qr/$spattern/ ) {
$push_act_line = 1;
if ( $estart ) {
$push_act_line = 0;
}
if (! $track ) {
$found_first=1;
}
$track = 1;
$counter++;
}
if ( $track && ! $found_first && defined($epattern) && $line =~ /$epattern/ ) {
$track = 0;
$push_act_line = 1;
if ( $eend ) {
$push_act_line = 0;
}
}
if ( $push_act_line ) {
push(@{$result->[$counter]},$line);
}
}
close(IN);
foreach my $rs (@{$result}) {
foreach my $line (@{$rs}) {
print $line."\n";
}
}
sub print_usage {
print "Usage: ".basename($0)." <--start-pattern> <--end-pattern> [--exclude-start] [--exclude-end] [--infile]";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment