1
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

4 に答える 4

1

awkを使用する1つの方法:

$ 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

于 2013-01-11T02:32:04.897 に答える
1
grep '#add#.*isTrial=true' input | sed 's/[^=]*=\([^ ]*\).*/\1/' | sort | uniq -c
于 2013-01-11T02:28:02.510 に答える
1

次のように、4番目のフィールドがまだ配列に含まれていないかどうかをテストする必要があります。

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)
}
于 2013-01-11T02:48:58.930 に答える
0
awk '$5~/isTrial=true/ && $3~/#add#/{a[$4]}END{for(i in a){count++}print count}'

ここでテスト済み

于 2013-01-11T07:46:29.327 に答える