Last active
August 29, 2015 14:19
-
-
Save M0ses/fe828c3a8548c6d4cd11 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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