下面是我的輸入文件以及我的輸出文件。需要幫助來讀取和寫入輸入文件。 (PS:輸入和輸出是同一個文件)PERL:在輸入文件中寫入(不覆蓋原始文件)
TS_dunit_ PDX_VER_6
TS_test1_par PDX_VER_0
我的代碼看起來像下面;
#!/usr/perl/5.14.1
use Getopt::Long;
use strict;
use warnings;
my $file;
GetOptions(
"iofile=s" => \$file
);
if (not defined $file){
print "Please specify input_output (iofile) file\n";
exit;
}
open (my $fh, "$file") or die "Can't open the file $file: ";
open (my $fh1, ">>$file") or die "Can't open the file $file: ";
while (<$fh>){
chomp $_;
next if ($_ !~ /S+/);
$_ =~ /(\S+)\s+(\S+)/;
my $first_underscore =index ($1, '_');
my $dev = substr ($1, $first_underscore + 1,
rindex ($1, '_') - $first_underscore - 1);
my $tag = $2;
my $cat_path = "/testdata/17.26.6/$dev/sd/$tag";
my $arc_path = "archive/$dev/sd/$tag";
if (-d $cat_path){
print $fh1 "$dev $tag IN_CAD\n";
}elsif (-d $arc_path){
print $fh1 "$dev $tag IN_ARCHIVE\n";
}else{
print $fh1 "NA\n";
}
}
print "Done! File been append.\n";
以上代碼給出的
TS_dunit_ PDX_VER_6
TS_test1_par PDX_VER_0
IN_CAD
IN_CAD
輸出需要幫助,如果反正我可以使輸出如下代替。
TS_dunit_ PDX_VER_6 IN_CAD
TS_test1_par PDX_VER_0 IN_CAD
文件_無法更改,以便添加到其行而不覆蓋它。無關緊要:您根據需要編寫輸出文件(附加行),然後將其重命名爲原始文件。最後,根據需要更改原始文件。 (在一個簡單的方法中,inode號碼將會改變,你是否在意這一點?) – zdim
非常感謝。真的很看重你的建議,我現在編碼... –