在我下面的代碼中,我試圖用相應數量的空格替換“---- ------”模式。當我運行代碼時,所有檔案內容都被清除了。我不明白為什么。我有下面的示例文本,其中要進行替換。我將不勝感激。
#!/usr/bin/perl -w
use strict;
use warnings;
use File::Path;
use File::Copy;
use feature 'unicode_strings';
use utf8;
my $IN;
my $dir;
opendir $dir, $Dirs[$mm] or die "opendir failed on $Dirs[$mm]: $! ($^E)";
while (my $file = readdir $dir) { #reading input directory
next if -d $file;
if ($file =~ /Ranked/) {
print "$file\n";
open ($IN, " > $Dirs[$mm]/$file") or die "open '$file': failed $! ($^E)";
while (<$IN>) {
s/---- ------/ /g;
print "$_\n";
}
close $dir;
exit;
}
}
END
Ranked Rainfall: Jun
********************
CHIPAT01 CHIPEP01 CHOMA001 ISOKA001 KABOMP01 KABWE001 KABWE002 KAFIRO01 KAFUE001 KALABO01 KAOMA001 KASAMA01 KASEMP01
Year Rnfl Year Rnfl Year Rnfl Year Rnfl Year Rnfl Year Rnfl Year Rnfl Year Rnfl Year Rnfl Year Rnfl Year Rnfl Year Rnfl Year Rnfl 1968 17.0 2003 13.2 2000 29.2 2004 3.1 1988 13.3 1988 2.8 ---- ------ 1967 2.8 1966 1.3 2009 2.3 1965 10.7 1968 11.9 1988 6.0 1983 5.8 2011 6.1 1966 14.0 2002 2.0 1965 9.4 1960 0.5 ---- ------ ---- ------ 1960 0.6 ---- ------ 2009 1.8 1952 0.8 1940 5.1 1974 4.6 1996 0.6 1997 4.5 ---- ------ 1966 2.3 1986 0.1 ---- ------ ---- ------ ---- ------ ---- ------ 1966 0.3 1936 0.3 1966 4.8 1960 3.3 2007 0.5 1988 4.0 ---- ------ Ave 0.5 Ave 0.1 ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ Ave 0.2 1965 1.8 1999 0.3 2002 3.7 ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ 1948 1.6 Ave 0.1 2003 3.6 ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ 1971 1.0 ---- ------ 1955 3.3 ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ 1978 0.9 ---- ------ 1976 2.5 ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ 1961 0.5 ---- ------ 1975 1.6 ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ 1946 0.5 ---- ------ 1978 1.6 ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ 1967 0.5 ---- ------ 2011 1.4 ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ Ave 0.4 ---- ------ 1960 1.3 ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ 1964 0.3 ---- ------ 2009 1.0 ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ 1996 0.6 ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ 1969 0.6 ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ Ave 0.5 ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ 1958 0.5 ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ 1979 0.3 ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------ ---- ------
uj5u.com熱心網友回復:
同時讀取和寫入同一個檔案需要小心。在這里,您使用的 >是同時讀取和寫入$file。但是,檔案open警告說:
您可以
在>或前面放置,<以指示您希望對檔案進行讀寫訪問;因此<對于讀/寫更新幾乎總是首選——該>模式將首先破壞檔案
相反,您應該寫入一個臨時檔案。完成寫入后,您可以用臨時檔案替換原始檔案。要打開臨時檔案,您可以使用該File::Temp模塊。要將原始檔案替換為新檔案,您可以使用movefromFile::Copy模塊。您的代碼已更正:
use File::Temp qw(tempfile);
use File::Copy;
while (my $file=readdir $dir) {
next if -d "$dir/$file";
if($file =~ /Ranked/){
print "$file\n";
my ($out_fh, $out_filename) = tempfile();
open (my $IN, "<", "$Dirs[$mm]/$file") or die "open '$file': failed $! ($^E)";
while( <$IN> ){
s/---- ------/ /g;
print $out_fh $_;
}
close $out_fh;
copy($out_filename, "$Dirs[$mm]/$file");
}
}
請注意,$out_fh在copying之前關閉很重要。
小貼士:
next if -d $file應該是next if -d "$dir/$file"。您應該在盡可能窄的范圍內宣告您的變數。這意味著,不是用
my $IN;和以后做開始你的腳本open $IN, ...,你應該做open my $IN, ...。對于同樣的事情my $dir和opendir。
替代解決方案:
Perl 具有用于就地檔案編輯的內置機制,但它更適合單行腳本而不是完整腳本。這是由
-i命令列 switch啟用的,但如果需要,它也可以在單行程式之外使用。一些 CPAN 包(例如
File::Inplace可用于避免必須自己撰寫整個tempfile/move樣板。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/381742.html
標籤:perl
