Skip to content

Instantly share code, notes, and snippets.

@DeeNewcum
Last active May 29, 2019 00:57
Show Gist options
  • Save DeeNewcum/5e6d87acf5bdea730ff7732354ba30f8 to your computer and use it in GitHub Desktop.
Save DeeNewcum/5e6d87acf5bdea730ff7732354ba30f8 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
@ARGV >= 1 or die "syntax: $0 <pattern> <file1> ...\n";
my $grep_pattern = shift @ARGV;
## break the file into sections
my $accum = '';
while (<>) {
if (/^.+:$/) {
if ($accum ne '') {
process_section($accum);
}
$accum = $_;
} else {
$accum .= $_;
}
}
if ($accum ne '') {
process_section($accum);
}
sub process_section {
my ($accum) = @_;
if (eval "\$accum =~ /$grep_pattern/om") {
print $accum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment