我有下面的腳本,需要在輸入文件,輸出文件和 用其他字符串替換輸入文件中的字符串,並輸出 輸出文件。如何進行批量搜索並用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);