Re: multiple-file search & replace
Guilherme Soares Zahn (IEN) wrote:
> I was wondering if there is a tool to help me search & replace a given
> keyword in all files inside a directory (and its subdirectories)... For
> instance, imagine that I wanted to change, in ALL makefiles of the Linux
> kernel source, the name of the compiler to use...
I like to type the following in zsh:
foreach i ( `echo **/Make*` )
replace '/bin/cc' '/usr/local/bin/cc' < $i > /tmp/tempfile;
mv /tmp/tempfile $i;
where replace is a perl script I slapped together that looks like below, and
the **/ construct recursively searches subdirectories. I would love to know
if someone has a better version, but this one at least keeps me from having
to remember lots of syntax all the time:
#!/usr/local/bin/perl5
###
### Created by Brian K. Boonstra
###
### Replace a
###
###
$/ = undef;
$find = @ARGV[0];
$replace = @ARGV[1];
if ($find eq "") {
print "replace <find-string> <replace-string>\n\nTake input fr
om stdin, replace the <find-string> with\n<replace-string>, and output to stdo
ut. The find and\nreplace strings should follow Perl syntax.\n\n";
exit(0);
}
$mystring = <STDIN>;
$mystring =~ s/$find/$replace/sg;
print $mystring;
Reply to: