-
Notifications
You must be signed in to change notification settings - Fork 13
/
syphon
executable file
·126 lines (93 loc) · 3.05 KB
/
syphon
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
126
#!/usr/bin/perl
=head1 NAME
syphon - removes lines from top of file
=head1 SYNOPSIS
$ syphon [-v] $regexp $src_filename > $outfile
$ syphon -i [-v] $regexp $src_filename [ $removed_file ]
=head1 DESCRIPTION
Removes lines from the beginning of a file. Stops when a pattern is
encountered (the first line matching the pattern is not removed).
Outputs the file minus the lines syphoned off to STDOUT, unless C<-i>
is specified, in which case the file is syphoned in place - the inode
is preserved by filtering to a temporary file and then copying back
over the original file. This means it will work with log files whose
daemon opens them with C<O_APPEND>. If C<$removed_file> is specified,
the removed lines will be written to that file.
C<-v> turns on verbose output.
=head1 EXAMPLE USAGE
syphon -i '^... Jul .. ........ ... 2010' get-fbcal.log get-fbcal.log-2010-06
Use of multi-syphon wrapper highly recommended!
=cut
use strict;
use warnings;
use File::Copy;
use File::Temp qw/ :mktemp /;
use Getopt::Long;
(my $me = $0) =~ s,.*/,,;
sub usage {
warn @_, "\n" if @_;
die <<EOF;
Usage: $me [-v] \$regexp \$src_filename > \$outfile
$me -i [-v] \$regexp \$src_filename [ \$removed_file ]
EOF
}
my %opts;
GetOptions(\%opts, 'in-place|i', 'verbose|v') or usage();
usage() unless @ARGV == 2 or ($opts{'in-place'} && @ARGV == 3);
my ($re, $src, $removed) = @ARGV;
my $compiled = qr/$re/;
open(FILE, $src) or die "$me: open($src): $!\n";
if ($removed) {
usage("$removed already exists.\n") if -e $removed;
open(REMOVED, ">$removed") or die "$me: open(>$removed): $!\n";
}
my ($temp_fh, $temp_fn) = get_dest();
remove_lines();
warn "Line removal finished.\n"
if $opts{'in-place'} && $opts{verbose};
print $temp_fh $_;
print $temp_fh $_ while <FILE>;
close(FILE);
if ($opts{'in-place'}) {
warn "Remainder written to $temp_fn.\n"
if $opts{verbose};
close($temp_fh);
# print "Written to $temp_fn\n";
copy $temp_fn, $src;
unlink $temp_fn;
}
sub remove_lines {
if ($removed) {
while (<FILE>) {
last if /$compiled/;
print REMOVED $_;
}
}
else {
while (<FILE>) {
last if /$compiled/;
}
}
}
sub get_dest {
return (\*STDOUT) unless $opts{'in-place'};
return mkstemp( "/tmp/syphon-XXXXXXX" );
}
=head1 SEE ALSO
L<csplit(1)>, which I only discovered after writing this :-(
I needed the "in-place" feature though.
=head1 VERSION
$Id$
=head1 LICENSE
Copyright (c) 2003 Adam Spiers <[email protected]>
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/>.
=cut