您需要测试以查看第四个字段是否不在数组中,如下所示:
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
这里有一些您可能感兴趣的信息。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)
}