Tiago Bruno Tiago Bruno - 2 months ago 7
Linux Question

How can I turn comma separated fields into different rows?

I have a file like that:

A 10,30,50,70 20,40,60,80 +


how can I make this become:

A 10 20 +
A 30 40 +
A 50 60 +
A 70 80 +


I tried using this awk command, but it did not work.

awk '{ split($2,a,","); for (i in a) print $1, a[i]; }'

Answer

Create another split-ed array b, where you kept the 3rd column elements:

echo "A 10,30,50,70 20,40,60,80 +" | awk '{ split($2,a,","); split($3,b,","); for (i in a) print $1, a[i],  b[i], $4; }'