我是perl的新手,发现这很难处理下面的情况和perl中哈希的概念。我找到了一些输入来处理这种情况,但没有安装Text:CSV模块。
#/usr/bin/perl -w
use strict;
use warning;
use Data::Dumper;
my $INFILE_01 = "FILE_01.txt";
my $INFILE_02 = "FILE_02.txt";
open INFILE02, "< $INFILE_02" or die "$0 : Failed to open input file $INFILE_02 : $! \n";
# Format of INFILE02 - Tab Delimited
DEVELOMENT_TEST BUGID_TS57 TestID DEVELOMENT_UAT BUGID_TS57 TestID DEVELOMENT_PROD BUGID_TS57 TestID DEVELOMENT_REG BUGID_TS57 TestID DEVELOMENT_TEST BUGID_TS57 TestID DEVELOMENT_TEST BUGID_TS57 TestID DEVELOMENT_UAT BUGID_TS57 TestID
my %INFILE02;
while (my $line = <INFILE02>) {
# Skipping if the line is empty or a comment
next if ( $line =~ /^\s*$/ );
next if ( $line =~ /^#\s*/ );
chomo $line;
if ($line = ~ /MOBILE_DEV/) {
my ($colum1, $column2, undef) = split /\t/, $line;
$INFILE02{$colum1} = $colum2;
}
}
close $INFILE02 or die "$0 : Failed to close input file $INFILE02 : $! \n";
print Dumper \%INFILE02;
# Output of %INFILE02
$VAR1 = {
'DEVELOMENT_TEST' => 'BUGID_TS57',
'DEVELOMENT_UAT' => 'BUGID_TS57',
'DEVELOMENT_PROD' => 'BUGID_TS57',
'DEVELOMENT_REG' => 'BUGID_TS57'
}
现在从INFILE02的哈希输出上面我需要将每行上的密钥DEVELOMENT_TEST,DEVELOMENT_UAT,DEVELOMENT_PROD,DEVELOMENT_REG与文件INFILE01相匹配。
对于Ex,如果INFILE01的任何一行上存在密钥DEVELOMENT_TEST,则将INFILE01的column1,第3列和INFILE02的所有列打印到输出文件。
我需要为%INFILE02的所有键重复此操作。
#Format of INFILE001 - Tab Delimited 119736 PRODUCTTESTING DEVELOMENT_TEST 448094 PRODUCTTESTING DEVELOMENT_TEST 206893 PRODUCTTESTING DEVELOMENT_TEST 333743 PRODUCTTESTING DEVELOMENT_TEST 172534 PRODUCTTESTING DEVELOMENT_PROD 785275 PRODUCTTESTING DEVELOMENT_PROD 995932 PRODUCTTESTING DEVELOMENT_PROD 223488 PRODUCTTESTING DEVELOMENT_TEST 433512 PRODUCTTESTING DEVELOMENT_TEST 640802 PRODUCTTESTING DEVELOMENT_PROD 403866 PRODUCTTESTING DEVELOMENT_UAT 828788 PRODUCTTESTING DEVELOMENT_UAT 751490 PRODUCTTESTING DEVELOMENT_UAT 972562 PRODUCTTESTING DEVELOMENT_UAT 367541 PRODUCTTESTING DEVELOMENT_REG 481360 PRODUCTTESTING DEVELOMENT_REG 920232 PRODUCTTESTING DEVELOMENT_UAT
所需的输出应如下所示:
119736 DEVELOMENT_TEST BUGID_TS57 TestID
448094 DEVELOMENT_TEST BUGID_TS57 TestID
206893 DEVELOMENT_TEST BUGID_TS57 TestID
333743 DEVELOMENT_TEST BUGID_TS57 TestID
223488 DEVELOMENT_TEST BUGID_TS57 TestID
433512 DEVELOMENT_TEST BUGID_TS57 TestID
172534 DEVELOMENT_PROD BUGID_TS57 TestID
785275 DEVELOMENT_PROD BUGID_TS57 TestID
995932 DEVELOMENT_PROD BUGID_TS57 TestID
640802 DEVELOMENT_PROD BUGID_TS57 TestID
403866 DEVELOMENT_UAT BUGID_TS57 TestID
828788 DEVELOMENT_UAT BUGID_TS57 TestID
751490 DEVELOMENT_UAT BUGID_TS57 TestID
972562 DEVELOMENT_UAT BUGID_TS57 TestID
920232 DEVELOMENT_UAT BUGID_TS57 TestID
367541 DEVELOMENT_REG BUGID_TS57 TestID
481360 DEVELOMENT_REG BUGID_TS57 TestID
请同意建议。提前谢谢。
答案 0 :(得分:0)
下面的内容,第一个参数是文件一,第二个参数是文件二:
#!/usr/bin/perl
use strict;
my %hash=undef;
open(my $fh,$ARGV[0]);
while (<$fh>) {
my @fields=split(/\s+/);
$hash{$fields[0]}=1;
}
close($fh);
open(my $fh,$ARGV[1]);
while (<$fh>) {
for my $key (keys %hash) {
if ($key && /$key/) {
print "$_";
}
}
}
close($fh);