我有下面的脚本,需要在输入文件,输出文件和 用其他字符串替换输入文件中的字符串,并输出 输出文件。如何进行批量搜索并用Perl替换?
我要改剧本通过文件 即不是提示输入和输出文件的目录遍历,脚本应该采取 作为参数的目录路径,如C:\ TEMP \ allFilesTobeReplaced \和 搜索对于字符串x并将其替换为y,以获取目录路径下的所有文件并写出相同的文件。
我该怎么做?
谢谢。
$file=$ARGV[0];
open(INFO,$file);
@lines=<INFO>;
print @lines;
open(INFO,">c:/filelist.txt");
foreach $file (@lines){
#print "$file\n";
print INFO "$file";
}
#print "Input file name: ";
#chomp($infilename = <STDIN>);
if ($ARGV[0]){
$file= $ARGV[0]
}
print "Output file name: ";
chomp($outfilename = <STDIN>);
print "Search string: ";
chomp($search = <STDIN>);
print "Replacement string: ";
chomp($replace = <STDIN>);
open(INFO,$file);
@lines=<INFO>;
open(OUT,">$outfilename") || die "cannot create $outfilename: $!";
foreach $file (@lines){
# read a line from file IN into $_
s/$search/$replace/g; # change the lines
print OUT $_; # print that line to file OUT
}
close(IN);
close(OUT);