vxp has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use Getopt::Std; getopt("d:s:r:"); print "dir: $opt_d search for: $opt_s replace with: $opt_r\n"; print "Is the above info correct? y/n: "; $confirmation = <>; if ($confirmation =~ /y/i) { opendir(DIR, $opt_d) or die "Couldn't opendir $opt_d\n"; while (defined($file = readdir(DIR))) { next if $file =~ /^\.\.?$/; # skip . and .. open(FILE, "$opt_d/$file") or die "Couldn't open $file +: $!\n"; while (defined($line = <FILE>)) { chomp $line; if ($line =~ /$opt_s/) { $line =~ s/$opt_s/$opt_r/; print "changed $opt_s to $opt_r in $fi +le\n"; } } close(FILE); } closedir(DIR); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: search-and-replace in files
by tune (Curate) on May 20, 2003 at 20:12 UTC | |
|
Re: search-and-replace in files
by Enlil (Parson) on May 20, 2003 at 20:15 UTC | |
|
(z) Re: search-and-replace in files
by zigdon (Deacon) on May 20, 2003 at 20:17 UTC | |
|
Re: search-and-replace in files
by freddo411 (Chaplain) on May 20, 2003 at 21:27 UTC | |
|
Re: search-and-replace in files
by wufnik (Friar) on May 21, 2003 at 14:13 UTC |