sp2 sp2 - 3 months ago 9
Bash Question

Converting matrix into a list using Shell script

I wanted to convert a matrix into a list using a shell script. Could anyone suggest me a way to do it using shell?

This is how a matrix looks like

hello a|b
hi b|c


Now I want a matrix like bellow:

hello a
hello b
hi b
hi c


Is there any way to do it using shell?

Thanks in advance

Answer

This command:

awk -F'[| ]+' '{for(i=2;i<=NF;i++)print $1 "\t" $i}' file

will convert:

hello a|b|c
hi    b|c|d

into:

hello   a
hello   b
hello   c
hi      b
hi      c
hi      d
Comments