2013-04-08 63 views
0

任何人都可以幫助我一個命令來識別特定列中的不同值嗎?命令來識別不同的字段值

對於如我輸入的是像

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

我需要像

Column1 
a 
b 
c 

我輸入文件中的產量頭。所以我需要一個將Column1作爲參數的命令。

回答

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 
+0

謝謝!需要更多的澄清..在這裏,我們知道column1是第一個字段incase,如果我們不知道哪個位置是column1可用那麼命令是什麼? – user2257712 2013-04-09 05:31:21

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

注意:不存在的列名稱會生成第一列中的值。