Reading input record from inside nawk

Hi friends,

I have small query with reg to awk search pattern..

below is my sample file and code which i tried..

$ cat file.txt
xxx,yyyyy,messageID,sha
xxxx,errorcode,messageID,name

in the above sample file - let assume I know the errorcode(2nd record) using which I want to find out sha record(1st line last column), the key is messageID which would be common for both the record.

I have tried following awk syntax but failing :frowning:

nawk -F"," '{if($0~/errorcode/) {var=$3;if($0~/var/ && $0!~/errorcode/) print $NF}}' file.txt

Thanks
SHa

---------- Post updated at 03:51 AM ---------- Previous update was at 03:47 AM ----------

Just to add above statement for better understanding...the record which I am searching in above syntax can be anywhere not always one line above or below...thats y i have not tried getline etc....

Thanks
Sha

something like:

#  nawk -F"," '{($2~"errorcode")  ? t[$3]=$2 :  s[$3]=$4 }END{for (x in t){print x,t[x],s[x]}}' infile
messageID errorcode sha

should be close to what you're after.

HTH

1 Like

Cool...worked perfectly...would you please also explain bit on for loop part how does it work....which is bit confusing me.........:frowning:

Anyway...Thanks a lot..

Thanks
SHa