以下内容按预期工作:
awk '$1 <= -700 { print $3 }' FS="," tmp | awk '!seen[$0]++'
23
60
73
91
Run Code Online (Sandbox Code Playgroud)
现在我计算这四个值并打印数字 4:
awk '$1 <= -700 { print $3 }' FS="," tmp | awk '!seen[$0]++' | awk '{ count++ } END { print count }'
4
Run Code Online (Sandbox Code Playgroud)
有没有一种更短的方法可以在一次调用中执行这三个 awk 调用?
非常感谢提示,
像这样:
awk '$1 <= -700 && !seen[$3]++ {c++} END{print c+0}' FS="," tmp
Run Code Online (Sandbox Code Playgroud)
解释:
# 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
}
Run Code Online (Sandbox Code Playgroud)