Ahmet Said Akbulut
Ahmet Said Akbulut

Reputation: 424

awk array is created but elements are missing

I have this sample file

userX   2020    start   id1
userY   2005    stop    id2
userZ   2006    start   id3
userT   2014    stop    id1
userX   2010    stop    id1

I want to create an array where year value $2 is element for every unique user-id pair $1$4 with given condition $3=="stop". For example arr[userXid1]=2010 and arr[userTid1]=2014

my code:

awk '{if($3=="stop") arr[$1$4]=$2} END{print arr[userXid1]}' log

expected output:

2010

But this prints empty line. when I print length(arr) it gives 3 which makes sense. but $2 values are not there and I can't figure out why. Any help is appreciated.


awk '{if($3=="start") arrstart[$1,$4]=$2; else if($3=="stop") arrstop[$1,$4]=$2 fi; next} END{for(i in arrstop) if(arrstart[i]>arrstop[i]) print i}' SUBSEP=':' log

addition: final code, nothing to do with question.

Upvotes: 1

Views: 89

Answers (2)

RavinderSingh13
RavinderSingh13

Reputation: 133610

With tac + awk combination you could try following to get only very last unique combination(s) of 1st, 4th field's value.

tac Input_file | awk '!arr[$1,$4]++ && $3=="stop"{print $2;exit}'

Explanation: Simple explanation would be, reverse the Input_file by tac first and then pass it as an standard input to awk program as input, in awk program print very first unique combination of $1,$4 then print 2nd field of that line and come out of the program immediately by exit.

OR you want to specifically look for keys id1 and userX respectively in 4th and 1st fields(with their last value only) then try following.

tac file1 | awk '$1=="userX" && $4=="id1" && $3=="stop"{print $2;exit}'

Upvotes: 1

anubhava
anubhava

Reputation: 785541

You may use this awk:

awk '$3 == "stop" {arr[$1,$4] = $2} END {print arr["userX","id1"]}' file

2010

To print all unique values use:

awk '$3 == "stop" {arr[$1,$4] = $2}
END {for (i in arr) print i, arr[i]}' SUBSEP=':' file

userY:id2 2005
userT:id1 2014
userX:id1 2010

Upvotes: 2

Related Questions