-
Notifications
You must be signed in to change notification settings - Fork 13
/
tailfgrep
executable file
·125 lines (106 loc) · 2.71 KB
/
tailfgrep
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env perl
#
# tailfgrep -- monitor a file a la tail -f, looking for a given regexp
# Copyright (c) 1999 Adam Spiers <adam@spiers.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# TODO:
# - extend to multiple patterns (one for each file?)
# - include more options from tail (e.g. -n) and grep (e.g. -i)
use strict;
use warnings;
use Getopt::Long;
my ($quiet, $inverse, $match) = (0, 0, 0);
GetOptions(
"quiet|q" => \$quiet,
"inverse|v" => \$inverse,
"match|m" => \$match,
);
unless (@ARGV) {
die <<EOF;
Usage: tailfgrep [ options ] <Perl regexp> <file1> [ <file2> [ <file3> ... ] ]
-m, --match -- echo everything to STDOUT, exit when first match found
-q, --quiet -- quiet mode, same as grep -q; don't print matches,
exit when first match found
-v, --inverse -- inverse mode (print lines which don't match)
EOF
}
my ($re, @files) = @ARGV;
my %fh = ();
open_files();
$| = 1;
my $last_file = '';
while (1) {
FILE:
foreach my $file (@files) {
my $fh = $fh{$file};
while (defined($_ = <$fh>)) {
if ($quiet) {
exit 0 if match($_);
}
elsif ($match) {
output_line($file);
exit 0 if match($_);
}
else {
output_line($file) if match($_);
}
}
seek($fh, 0, 1); # clear EOF
}
sleep 1;
}
close_files();
sub open_files {
if (! @files && -t 1) {
push @files, '-';
}
foreach my $file (@files) {
if ($file eq '-') {
$fh{$file} = *STDIN;
next;
}
-f $file or die "$file isn't a file.\n";
local *FH;
open(FH, $file) or die "Couldn't open $file: $!\n";
seek(FH, 0, 2);
$fh{$file} = *FH;
}
}
sub close_files {
foreach my $file (@files) {
my $fh = $fh{$file};
# warn "closing $file / $fh\n";
close($fh);
}
}
sub match {
my ($line) = @_;
return ($line =~ /$re/) ^ $inverse;
}
sub output_line {
my ($file) = @_;
$file = 'STDIN' if $file eq '-';
if (@files > 1) {
if ($file ne $last_file) {
print "\n==> $file <==\n\n";
}
print;
$last_file = $file;
}
else {
print;
$last_file = $file;
}
}