Parse key-value pair into separate rows

Hi,

I'm getting key-value pairs in a string as follows -

0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405552747,9700005405717924,9700005405733788|unidentified,unidentified,unidentified

I need output as follows -

row1:
0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405552747|unidentified

row2:
0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405717924|unidentified

row3:
0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405733788|unidentified

There are more columns but I have pasted only two.

Any help is greatly appreciated.

Thanks,
Suyog

Will this work for you?

awk -F'[\|,]' ' { printf "%s|%s|%s|%s\n%s|%s|%s|%s\n%s|%s|%s|%s\n",$1,$2,$3,$6,$1,$2,$4,$7,$1,$2,$5,$8 }' infile

That works thanks.

but, is there a way to generalize it ? there's no fixed number of key-value pairs.

Generalized:-

awk -F'[\|,]' ' { f=3; s=(NF/2)+2; while(s<=NF) { printf "%s|%s|%s|%s\n",$1,$2,$f,$s; f++;s++; }  } ' infile
1 Like

Thanks Bipin.

I'm building on it to meet my requirement. I will keep you posted thanks.

$ cat t1
0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405552747,9700005405717924,9700005405733788|unidentified,unidentified,unidentified
0000xm7zcNDIkP888vRqGvZZZZZZZZZZZZZZZ||9700005405552747,9700005405717924,9700005405733788|unidentified,unidentified,unidentified
0000xm7zcNDIkP888vRqGvZZZZZZZZZZZZZZZ||9700005405717924,9700005405733788|unidentified,unidentified,unidentified
0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405552747,9700005405717924,9700005405733788|unidentified,unidentified,unidentified
$ awk -F"|" '{a=$1
m=split($(NF-1),V,",")
n=split($NF,T,",")
if(n!=m){print "ERROR-File[" FILENAME "] -line[" NR "] : Number of field inconsistent";exit}
else{i=0
while(++i<=m){print "row"(++d)":" RS a FS FS V FS T}
}}' t1
row1:
0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405552747|unidentified
row2:
0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405717924|unidentified
row3:
0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405733788|unidentified
row4:
0000xm7zcNDIkP888vRqGvZZZZZZZZZZZZZZZ||9700005405552747|unidentified
row5:
0000xm7zcNDIkP888vRqGvZZZZZZZZZZZZZZZ||9700005405717924|unidentified
row6:
0000xm7zcNDIkP888vRqGvZZZZZZZZZZZZZZZ||9700005405733788|unidentified
ERROR-File[t1] -line[3] : Number of field inconsistent
$

If you don't want to exit when encountering an inconsistency in the number of field you can just replace the "exit" with "next" to skip the erroneous line.