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.
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