-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblkgrep
More file actions
executable file
·38 lines (31 loc) · 1.36 KB
/
blkgrep
File metadata and controls
executable file
·38 lines (31 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/perl -w
#Author: James Washer washer@trlp.com
use constant VERSION => '$Revision: 1.3 $';
#bgrep:: Block based grep utility. Search for a pattern in a block of text.
#dumps entire block if match found
#sort of like csplit and grep combined into one utility.
use strict;
my @data;
my $interesting;
my $saw_seperator; #just to avoid end case where we never saw a seperator;
die "bgrep.pl ", VERSION,"\n" if (1==@ARGV && "-v" eq $ARGV[0]);
die "Usage bgrep.pl block_seperator_regex grep_search_regex\n" unless 2==@ARGV; #Must have at least 2 arguments
my ($seperator,$search)= @ARGV;
@ARGV=();
while (<>){
if ( /$seperator/){
$saw_seperator++; #This is just used to handle the end-of-file case where we may never have seen the beginning seperator
#We're about to start a new block, so process previous
process() if $interesting;
@data=(); #clear out our stored data to be ready for next block
$interesting=0; #nothing interesting yet, since new block
}
$interesting++ if /$search/; #mark this block interesting if match
push @data, $_;
}
process() if ($saw_seperator &&$interesting); #one last time for end of file
sub process{
#Generally, we just print this "interesting" block
#but feel free to munge on this block if needed
print foreach ( @data);
}