我正試圖讀取由數百個文件組成的目錄。對於目錄中的每個文件,我應該訪問(編輯)另一個文件,並通過'/ home/test/test_these_files'目錄中的文件名'information ='替換該文件中的特定字符串'information = filename'。接着執行文件'/home/test/controlfile.txt'。我寫了下面的腳本,並嘗試將文件的名稱作爲數組傳遞以替換(字符串中的文件名)。我知道這不會工作,但我不知道什麼會工作。有什麼建議麼?僅供參考:程序不顯示任何編譯錯誤。如何在需要替換的字符串中輸入不同的文件名?
#!use/bin/perl
use strict;
use warnings;
my $find = 'information=filename';
opendir (DIR, "/home/test/test_these_files") || die "can't open directory";
my @files = grep(/.aa/, readdir(DIR));
print @files;
print "\n";
closedir(DIR);
foreach (@files) {
my $replace = 'information=<@files>'; # want to replace with 'information=<filenames>
#filenames are names of files present in the '/home/test/test_these_file' directory
open (FILE, '/home/test/controlfile.txt') || die "cant open file \n";
my @body = <FILE>;
print @body;
open (OUT, '>/home/test/controlfile.txt') || die "can't create file \n";
foreach $_ (@body) {
(s/$find/$replace/g);
print OUT "$_";
}
'perl /home/test/controlfile.txt';
close(FILE);
close(OUT);
}
exit;
縮進是你的朋友。什麼是''perl /home/test/controlfile.txt';`應該這樣做? $ replace被設置錯誤。 – Schwern 2009-06-03 04:08:46