Skip to content

Instantly share code, notes, and snippets.

@wh13371
Created February 18, 2016 16:07
Show Gist options
  • Save wh13371/7e71fb44376a6bfa2ac5 to your computer and use it in GitHub Desktop.
Save wh13371/7e71fb44376a6bfa2ac5 to your computer and use it in GitHub Desktop.
perl - flipflop
# tiny "flip-flop"
use strict;
use warnings;
use 5.020;
use Getopt::Long qw(GetOptions);
Getopt::Long::Configure ("bundling"); # -lcd => line / chomp / debug
my $debug;
my $chomp;
my $line;
GetOptions('debug|d' => \$debug, 'chomp|c' => \$chomp, 'line|l' => \$line) or die "$0";
#say $debug ? 'debug' : 'no debug';
if ($debug) { if ( !@ARGV ) { say "USAGE: flipflop.pl <start_regex> <stop_regex> LOG"; exit 46; } }
if ($debug) { for ( @ARGV ) { say $_; } }
my $start_regex = shift;
my $stop_regex = shift;
while (<>) {
$_ =~ s/^\s*(.*?)\s*$/$1/ if $chomp;
if ( /$start_regex/ .. /$stop_regex/ ) # flip-flop
{
if ($line)
{
say $. . ":" . $_; # prefix line
}
else
{
say $_; # no line
}
}
}
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment