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

I want to count the lines which are having unique ids with #add# attribute in them & having isTrial=true.

This is my current solution and I want to know why my array is not printing

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 Answers 4

1

You need to test to see if the fourth field is not already in the array, like so:

BEGIN {
    print "Begin Processing of various Records"
}

$3 ~ /add/ && $5 ~ /true/ && !a[$4]++ {

    i++
    print
}

END {
    print "Process Complete. Records found:", i
}

Results:

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 that may interest you. HTH.


As per comments below, you could also do this:

BEGIN {
    print "Begin Processing of various Records"
}

$3 ~ /add/ && $5 ~ /true/ && !a[$4] {

    a[$4]++
    print 
}

END {
    print "Process Complete. Records found:", length(a)
}

Note that this is very different from:

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)
}
Sign up to request clarification or add additional context in comments.

10 Comments

Can you please explain what does this means !a[$4]++
Sure. It simply means: 'If field 4 is not (!) in an array (called a), add it to the array, incrementing the key's value by one (++)'. Does that make sense? The fourth field is the key to the array.
I didn't get the ++ part , i didn't get what part is adding the $4 to array and what part is incrementing the key
So, $4 is the key. And a[$4] would be the value of that key. As soon as you write a[$4]++ the key is 'put' into the array and assigned a value of one. We use ++ to increment the value of the key, so that each time the key is found, the key's value is increased by one. You may also find this a worthwhile read. Please let me know if more explanation is needed. Cheers!
I still didn't get it. why we need to increment the value and for what purpose we are using that value. what diff it makes if value does not increment. All we are concerned is the if key is in array or not. we have no intial value of a['id=3213313']. now by incrementing you mean a['id=313123' + 1] or a['id='313123'] = 0 + 1
|
1
grep '#add#.*isTrial=true' input | sed 's/[^=]*=\([^ ]*\).*/\1/' | sort | uniq -c

1 Comment

Actually i am learning awk , so i would like awk solution to learn. thanks for that
1

One way using awk:

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

Regarding your solution:

  1. When you use the contains(~) operator, the pattern should always be provided in slashes(//), not directly in double quotes.

  2. When you check $4 not in arr, it checks for $4 in the array keys, whereas you are populating $4 as an array value arr[i]=$4.

1 Comment

Can you correct my script , so that i see how far i was from the solution
0
awk '$5~/isTrial=true/ && $3~/#add#/{a[$4]}END{for(i in a){count++}print count}'

tested here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.