sed application?
Kevin Buettner
plug-discuss@lists.plug.phoenix.az.us
Thu, 13 Feb 2003 13:13:55 -0700
On Feb 13, 1:03pm, Mike Starke wrote:
> I have a case where I need to change the value
> of a variable in a number of Perl scripts.
>
> How would I take the results of a grep command
> and apply a change to it? For instance, if I
> issue the command:
>
> 'grep -r smtp_addr *'
>
> and get a bunch of results like:
>
> 1stfile: $smtp_adrr = "old_value";
> 2ndfile: $smtp_adrr = "old_value";
> 3rdfile: $smtp_adrr = "old_value";
> 4thfile: $smtp_adrr = "old_value";
Why not just use perl? The following script is probably overkill for
what you want to do, but I use scripts like it on a regular basis to
make edits in multiple files. If you're unsure about what you're
doing, you may wish to give $INPLACE_EDIT some other value than the
null string. E.g, you may wish to do
$INPLACE_EDIT = '.bak';
instead.
--------- snip --------
#!/usr/bin/perl -w
use File::Find;
use FileHandle;
use IPC::Open3;
use English;
my ($root) = @ARGV;
if (!defined($root)) {
die "Usage: $0 root\n";
}
@ARGV = ();
find(
sub {
if (-f && -T) {
push @ARGV, $File::Find::name;
}
},
$root
);
$INPLACE_EDIT = '';
undef $/; # slurp entire files
while (<>) {
s/(\$smtp_adrr = )"old_value"/$1"new_value"/gx;
print;
}