如何在Linux中使用带有awk的数组

时间:2013-01-11 02:15:56

标签: linux arrays awk

INFO #my-service# #add# id=67986324423 isTrial=true
INFO #my-service# #add# id=43536343643 isTrial=false
INFO #my-service# #add# id=43634636365 isTrial=true
INFO #my-service# #add# id=67986324423 isTrial=true
INFO #my-service# #delete# id=43634636365 isTrial=true
INFO #my-service# #delete# id=56543435355 isTrial=false

我想计算具有#add#属性的唯一ID的行和&拥有isTrial=true

这是我目前的解决方案,我想知道为什么我的阵列没有打印

BEGIN { print "Begin Processing of various Records"}

{if($3~"add" && $5~"true")
   {
   ++i; 
   if($4 not in arr){arr[i]=$4;++j} 
   }
  {print $0}
}

 END {print "Process Complete:--------"j}

4 个答案:

答案 0 :(得分:1)

grep '#add#.*isTrial=true' input | sed 's/[^=]*=\([^ ]*\).*/\1/' | sort | uniq -c

答案 1 :(得分:1)

使用awk的一种方式:

$ awk '$3 ~ /add/ && $5 ~ /true/{sub(/.*=/,"",$4);a[$4]++;}END{for (i in a)print i, a[i];}' file
43634636365 1
67986324423 2

关于您的解决方案:

  1. 使用contains(~)运算符时,模式应始终以斜杠(//)提供,而不是直接用双引号提供。

  2. 当您检查$4 not in arr时,它会检查数组键中的$ 4,而您将$ 4填充为数组值arr[i]=$4

答案 2 :(得分:1)

您需要测试以查看第四个字段是否已经在数组中,如下所示:

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)
}

答案 3 :(得分:0)

awk '$5~/isTrial=true/ && $3~/#add#/{a[$4]}END{for(i in a){count++}print count}'

测试here