您需要測試,看看第四場是不是已經在陣列中,像這樣:
BEGIN {
print "Begin Processing of various Records"
}
$3 ~ /add/ && $5 ~ /true/ && !a[$4]++ {
i++
print
}
END {
print "Process Complete. Records found:", i
}
結果:
Begin Processing of various Records
INFO #my-service# #add# id=67986324423 isTrial=true
INFO #my-service# #add# id=43634636365 isTrial=true
Process Complete. Records found: 2
Here's some info您可能會感興趣。 HTH。
按照下面的評論,你也可以這樣做:
BEGIN {
print "Begin Processing of various Records"
}
$3 ~ /add/ && $5 ~ /true/ && !a[$4] {
a[$4]++
print
}
END {
print "Process Complete. Records found:", length(a)
}
請注意,這是非常不同:
BEGIN {
print "Begin Processing of various Records"
}
$3 ~ /add/ && $5 ~ /true/ && !a[$4] {
# See the line below. I may not have made it clear in the comments that
# you can indeed add things to an array without assigning the key a
# value. However, in this case, this line of code will fail because our
# test above (!a[$4]) is testing for an absence of value associated
# with that key. And the line below is never assigning a value to the key!
# So it just won't work.
a[$4]
# Technically, you don't need to increment the value of the key, this would
# also work, if you uncomment the line:
# a[$1]=1
print
}
END {
print "Process Complete. Records found:", length(a)
}
你能解釋一下這是什麼意思'!a [$ 4] ++' – user175386049
當然。它只是意味着:'如果字段4不是(!)在一個數組(稱爲a)中,將它添加到數組中,將該鍵的值遞增1(++)'。那有意義嗎?第四個字段是數組的關鍵。 – Steve
我沒有得到++部分,我沒有得到什麼部分是將4美元添加到數組,以及哪部分是遞增密鑰 – user175386049