我有一个包含以下内容的文件:
H200:主机名1,主机名2,hostname3
H400:hostname4,主机名1,hostname5
H500:主机名2,的hostname6,hostname4,hostname7
H700:hostname8,hostname5,主机名2,hostname7
我需要从整个文件中删除重复的主机名,输出应该如下所示,其中每行包含唯一的主机名:
H200:主机名1,主机名2,hostname3
H400:hostname4,hostname5
H500:的hostname6,hostname7
H700:hostname8
答案 0 :(得分:0)
这是perl中的一种方式。
counter
您可以将其保存到名为BEGIN{
%hosts=(); # keep track of all hostnames
%labels=(); # associate hostname with label
}
chomp;
# $label = H200, $hosts = hostname1,hostname2,hostname3
($label,$hosts) = split(/:/);
# for every hostname
foreach my $h (split(/,/,$hosts)) {
# if we haven't see it yet, associate it with label and mark as seen.
if(!defined $hosts{$h}){
push(@{$labels{$label}}, $h);
$hosts{$h} = 1;
}
}
END{
# output each label's hostname list
foreach my $label (sort keys %labels){
print "$label:" . join(",", @{$labels{$label}}) . "\n";
}
}
的文件中,然后运行:
unique