#!/usr/bin/perl -w # # substitute, 04/12/2000-07/08/2008, Peter Csizmadia # use strict; use File::stat; use Getopt::Std; use vars qw / $opt_p $opt_v $opt_m /; my $firstNlines = 0; for(my $i = @ARGV - 1; $i >= 0; --$i) { if($ARGV[$i] =~ /^-\d+$/) { $_ = $ARGV[$i]; s/-(\d+)/$1/; $firstNlines = $_; splice @ARGV, $i, 1; } } getopts('pvm:'); my $preserveTime = $opt_p; my $verbose = $opt_v; my $multiLine = $opt_m; if($multiLine) { if(!($multiLine eq "x") && !($multiLine eq "i")) { printf STDERR "unknown -m argument \"%s\"\n", $multiLine; exit 1; } } my $substcmd = ""; my $regexp = ""; my $regexp_end = ""; my $replacement = ""; if(!$multiLine && $#ARGV >= 1 && $ARGV[0] =~ /^s\/.*\/.*\//) { $substcmd = $ARGV[0]; } if(($multiLine && $#ARGV < 3) || (!$multiLine && (($substcmd && $#ARGV < 1) || (!$substcmd && $#ARGV < 2)))) { print < $tmpfile") or die "$tmpfile: cannot open"; if($multiLine) { my $lcount = 1; my $copy = 1; while() { my $line = $_; if($firstNlines && $lcount > $firstNlines && $copy) { print OUT; ++$lcount; next; } if(/$regexp/) { if($copy) { open(IN2, $replacement) or die "$replacement: cannot open"; while() { print OUT; } close(IN2) or die "$replacement: cannot close"; $copy = 0; if($verbose) { print "<$filename:$lcount-:$line"; } $changed = 1; } } elsif(/$regexp_end/ && !$copy) { if($verbose) { print "<$filename:-$lcount:$line"; } if($multiLine eq "x") { print OUT; } $copy = 1; } elsif($copy) { print OUT; } ++$lcount; } } else { my $lcount = 1; while() { my $line = $_; if($firstNlines && $lcount > $firstNlines) { print OUT; ++$lcount; next; } my $matches = 0; if($substcmd) { $matches = eval $substcmd; } else { $matches = s/$regexp/$replacement/g; } if($matches && !($_ eq $line)) { if($verbose) { print "<$filename:$lcount:$line"; print ">$filename:$lcount:$_"; } $changed = 1; } print OUT; ++$lcount; } } close(OUT) or die "$tmpfile: cannot close"; close(IN) or die "$filename: cannot close"; if($changed) { if(!$verbose) { printf("%s\n", $filename); } rename($tmpfile, $filename) or die "$tmpfile: cannot rename"; if($preserveTime) { utime $sb->atime, $sb->mtime, $filename; } } else { unlink($tmpfile) or die "$tmpfile: cannot unlink"; } }