Raymond C.
Raymond C.

Reputation: 598

Awk associative array increment

I have a awk command which I am trying to understand what it does. I have duplicated the file from etc/passwd.

I have did some research that the $4 is the forth column, which is the group. The a[$4] loads all the items from $4 into a associative array. However, I don't understand what does the ++== do.

awk -F: 'a[$4]++==2 { print $4 }' passwd     

Many thanks!

Upvotes: 1

Views: 456

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133650

Following is the explanation for shown code by OP, only for explanation purposes not for running the code it is.

-F:                       ##Setting field separator as colon here.
a[$4]++                   ##Creating array a with index/key 4th field of current line and ++ will do increment to its same occurrence here, to get to know how many times same 4th field has occurred.
==2                       ##Then checking condition if it has count 2 here(occurrence of same 4th field in array a has its count 3(since we are doing post increment)) then do following.
{ print $4 }' passwd      ##Printing 4th field of that line.

1 thing here, even your Input_file has more than 3 occurrences of 4th field it is going to print 3rd occurrence only so in case you want to print all those Lines where 4th field comes 2 or more times then change your code to:

awk -F: '++a[$4]>=2 { print $4 }' passwd     


Example of array execution:

Let's say we have following Input_file:

cat Input_file
a,b,c,1st,bla bla
a,b,c,2nd,bla bla
a,b,c,1st, bla bla

Now creating array with a[$4]++ will save values inside array like this:

a[1st]=2, a[2nd]=1 and so on for OP's understanding I am showing it like this fashion.

We could get and understand this with a for loop which traverse inside array like:

awk 'BEGIN{FS=","} {a[$4]++} END{for(key in a){print "Index of array is:" key " And value is:" a[key]}}'  Input_file
Index of array is:2nd And value is:1
Index of array is:1st And value is:2

Upvotes: 6

Related Questions