以下按預期作業:
awk '$1 <= -700 { print $3 }' FS="," tmp | awk '!seen[$0] '
23
60
73
91
現在我計算這四個值并列印數字 4:
awk '$1 <= -700 { print $3 }' FS="," tmp | awk '!seen[$0] ' | awk '{ count } END { print count }'
4
有沒有更短的方法在一次呼叫中完成這三個 awk 呼叫?
非常感謝提示,
uj5u.com熱心網友回復:
像這樣:
awk '$1 <= -700 && !seen[$3] {c } END{print c 0}' FS="," tmp
解釋:
# If column 1 <= -700 and we've not seen the value of column 3 yet ...
$1 <= -700 && !seen[$3] {
# ... increment the counter c
c
}
# When the end of the input file is reached, print the counter
END {
# Note: incrementing the counter by 0 ensures that c
# has the value 0 when no line matched the criterias and thereby
# c has never been incremented. Without this, c would be an
# empty string. This gets often forgotten. Thanks @Ed Morton!
# Alternatively you may run the program as awk -v c=0 ...
print c 0
}
uj5u.com熱心網友回復:
計數值?只需將值放入陣列并列印長度,您不需要列印任何內容。
awk '$1 <= -700 { uniq[$3] } END{ print length(uniq) }'
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/372398.html
