2013-02-01 22 views
1

最近,我在統計字段中重複值的次數方面取得了一些很大的幫助,但我的下一步是計算一個值的次數基於在另一字段($ 3),在該行的末尾的結果值的字段($ 1)重複如下面的例子:統計一個字段中的值出現在另一個字段中的次數

輸入文件
1,2,3
1,1 ,1
3,2,3
4,1,4
2,1,3
5,2,2
5,1,5
5,4,6

1,2,3,1  
1,1,1,2  
3,2,3,1  
4,1,4,1  
2,1,3,1  
5,2,2,1  
5,1,5,3  
5,4,6,0 

我期待在使用awk如果可能的話,但很高興其他任何建議,這樣的輸出文件。

+0

你能澄清這'的值被重複了多少遍的字段( $ 1)根據另一個字段的值($ 3)'?我無法理解你的意思... – user000001

+0

我與@ user828193,我不知道你想要做什麼。我以爲我做到了,但那時你的預期產出就像我預料中的那樣。 –

回答

1

下面是使用awk一個辦法:

awk -F, 'FNR==NR { a[$1]++; next } { print $0, ($3 in a ? a[$3] : "0") }' OFS=, file file 

結果:

1,2,3,1 
1,1,1,2 
3,2,3,1 
4,1,4,1 
2,1,3,1 
5,2,2,1 
5,1,5,3 
5,4,6,0 

說明:

FNR==NR { ... } # for the first file in the arguments list 

a[$1]++   # add column one to an array incrementing it's value. 

next    # skip processing the rest of the code 

{ ... }   # for every line in the second file in the arguments list 

print $0   # print the line 

($3 in a ? a[$3] : "0") # ...followed by the value of the third field in the 
          # array if it is indeed in the array, else print "0". 
          # this is a ternary operator, much like an if/else 
          # statement 
+0

啊,現在我明白了。希望我可以給你超過+1的數據來判斷這一點! –

+0

不需要三元表達式,只需要'打印$ 0,一個[$ 3] + 0'。 –

+0

謝謝埃德 - 我希望我會記得實現這個訣竅。我完全同意;這些要求非常難以理解,而且措辭可能更好。 – Steve

相關問題