Root/bin/grepchunk

Source at commit a9ee51921bbb2435a8b3df66b38f3029d288f264 created 9 years 11 months ago.
By Werner Almesberger, ircstat/ML: update for 2014-03
1#!/usr/bin/perl
2
3
4sub usage
5{
6    print STDERR "usage: $0 [-v] pattern [file ...]\n";
7    exit(1);
8}
9
10
11if ($ARGV[0] eq "-v") {
12    $invert = 1;
13    shift @ARGV;
14}
15&usage if $ARGV[0] =~ /^-/;
16
17$pattern = shift @ARGV;
18&usage unless defined $pattern;
19
20
21sub flush
22{
23    my $mismatch = !($chunk =~ /$pattern/m);
24
25    if ($mismatch == $invert) {
26        print $head;
27        print $chunk;
28    }
29    undef $head;
30    undef $chunk;
31}
32
33
34$HEAD = 0;
35$CHUNK = 1;
36$TAIL = 2;
37
38
39$state = $HEAD;
40while (<>) {
41    $state = $HEAD if /^diff /;
42    if (/^@@ -\d+,(\d+) \+\d+,(\d+)/) {
43        $state = $CHUNK;
44        ($in, $out) = ($1, $2);
45        $chunk = $_;
46        next;
47    }
48    if ($state == $HEAD) {
49        $head .= $_;
50        next;
51    }
52    next if $state == $TAIL;
53    $in-- if /^-/;
54    $out-- if /^\+/;
55    ($in--, $out--) if /^\s/;
56    die "in $in out $out" if $in < 0 || $out < 0;
57    if ($in+$out == 0) {
58        $state = $TAIL;
59        &flush;
60    } else {
61        $chunk .= $_;
62    }
63}
64&flush;
65

Archive Download this file

Branches:
master



interactive