pali pali - 3 months ago 7
Perl Question

Parse a tab and comma separated file

I have a table containing several thousands line like this

A GO:0008150,GO:0050789,GO:0050794,GO:0051726,GO:0065007
B GO:0008150,GO:0050789,GO:0050794,GO:0051726,GO:0065007


I want to parse my table in the following format.

A GO:0008150
A GO:0050789
A GO:0050794
A GO:0051726
A GO:0065007
B GO:0008150
B GO:0050789
B GO:0050794
B GO:0051726
C GO:0065007


Any help will be greatly appreciated. Thanks

Answer

Easy with awk: just split() the second column and loop through the slices:

$ awk '{n=split($2, a, ","); for (i=1;i<=n;i++) print $1,a[i]}' file
A GO:0008150
A GO:0050789
A GO:0050794
A GO:0051726
A GO:0065007
B GO:0008150
B GO:0050789
B GO:0050794
B GO:0051726
B GO:0065007
Comments