0

任何人都可以帮助我识别特定列中的不同值吗?

例如,我的输入就像

Column1 Column2 Column3
a   11  abc
a   22  abc
b   33  edf
c   44  ghi

我需要像这样的输出

Column1
a
b
c

我的输入文件有标题。所以我需要一个命令,我们将 Column1 作为参数传递。

4

2 回答 2

0

文件distinct.pl

#!/usr/bin/perl
$_ = <STDIN>;
@F = split;
map $col{$F[$_]}=$_, (0..$#F);          # map column names to numbers
while (<STDIN>)
{
    @F = split;
    $val{$F[$col{$ARGV[0]}]} = undef    # implement the set of values
}
$, = "\n";
print $ARGV[0], "";                     # output the column parameter
print sort(keys %val), ""               # output sorted set of values

示例命令:distinct.pl Column2 <input

注意:不存在的列名会产生第一列的值。

于 2014-06-27T08:40:32.460 回答
0

使用输入文件运行以下命令:

$ head -1 input.file | awk '{ print $1}'; awk '{ if (NR > 1) print $1 }' input.file | uniq
Column1
a
b
c

要不就:

$ awk '{print $1 }' input.file | uniq
Column1
a
b
c
于 2013-04-08T13:22:19.393 回答