0

我想知道您是否可以帮我找到解决问题的方法(不一定要给我代码)。我想在 perl 或 Bash 中创建一个“匹配矩阵”。

基本上,我的第一个文件是一个提取的 ID 列表,不是唯一的(file1)

ID1 
ID4 
ID20 
ID1 

为了让我的生活更轻松,我的第二个文件只是一个带有多个 ID 的长行(file2)

ID1 ID2 ID3 ID4 .... IDn

我想实现这个输出:

    ID1 ID2 ID3 ID4 ID5 ID6 ID7 .... ID20 IDn
ID1  X
ID4              X
ID20                                  X
ID1  X

对我来说棘手的部分是在找到匹配项时添加“X”。

任何帮助,提示都非常感谢。

4

1 回答 1

0

这是我对这个问题的回答:

use warnings;
use strict;

open(FILE1, "file1") || die "cannot find file1";
open(FILE2, "file2") || die "cannot find file2";

#read file2 to create hash
my $file2 = <FILE2>;
$file2 =~ s/\n//;
my @ids = split(/\s+/, $file2);
my %hash;
my $i = 1;
map {$hash{$_} = $i++;} @ids;

#print the first line
printf "%6s",'';
for my $id (@ids) {
    printf "%6s", $id;
}
print "\n";

#print the other lines
while(<FILE1>) {
    chomp;
    my $id = $_;
    printf "%6s", $id;
    my $index = $hash{$id};
    if ($index) {
        $index = 6 * $index;
        printf "%${index}s\n",'x';
    } else {
        print "\n";
    }
}
于 2013-07-26T11:12:52.210 回答