Skip to content
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
Cannot retrieve contributors at this time
executable file 126 lines (93 sloc) 3.05 KB
=head1 NAME
syphon - removes lines from top of file
$ syphon [-v] $regexp $src_filename > $outfile
$ syphon -i [-v] $regexp $src_filename [ $removed_file ]
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.
syphon -i '^... Jul .. ........ ... 2010' get-fbcal.log get-fbcal.log-2010-06
Use of multi-syphon wrapper highly recommended!
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 ]
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();
warn "Line removal finished.\n"
if $opts{'in-place'} && $opts{verbose};
print $temp_fh $_;
print $temp_fh $_ while <FILE>;
if ($opts{'in-place'}) {
warn "Remainder written to $temp_fn.\n"
if $opts{verbose};
# 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
=head1 LICENSE
Copyright (c) 2003 Adam Spiers <>
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
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 <>.