【发布时间】:2017-08-14 15:31:28
【问题描述】:
下面是我的输入文件以及我的输出文件。需要帮助来读取和写入输入文件。 (PS:输入输出是同一个文件)
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 号会改变,你在乎吗?)
-
非常感谢。真的很重视你的建议,我现在正在编码......