1 中的三个 awk 调用
Three awk calls in 1
以下按预期工作:
awk ' <= -700 { print }' FS="," tmp | awk '!seen[[=10=]]++'
23
60
73
91
现在我计算这四个值并打印数字 4:
awk ' <= -700 { print }' FS="," tmp | awk '!seen[[=11=]]++' | awk '{ count++ } END { print count }'
4
有没有更短的方法可以在一次调用中完成这三个 awk 调用?
非常感谢提示,
计数值?只需将值放入数组并打印长度,您不需要打印任何内容。
awk ' <= -700 { uniq[] } END{ print length(uniq) }'
像这样:
awk ' <= -700 && !seen[]++ {c++} END{print c+0}' FS="," tmp
解释:
# If column 1 <= -700 and we've not seen the value of column 3 yet ...
<= -700 && !seen[]++ {
# ... 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
}
以下按预期工作:
awk ' <= -700 { print }' FS="," tmp | awk '!seen[[=10=]]++'
23
60
73
91
现在我计算这四个值并打印数字 4:
awk ' <= -700 { print }' FS="," tmp | awk '!seen[[=11=]]++' | awk '{ count++ } END { print count }'
4
有没有更短的方法可以在一次调用中完成这三个 awk 调用?
非常感谢提示,
计数值?只需将值放入数组并打印长度,您不需要打印任何内容。
awk ' <= -700 { uniq[] } END{ print length(uniq) }'
像这样:
awk ' <= -700 && !seen[]++ {c++} END{print c+0}' FS="," tmp
解释:
# If column 1 <= -700 and we've not seen the value of column 3 yet ...
<= -700 && !seen[]++ {
# ... 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
}