convert row to column with respect of first column.

Input file A.txt :-

C2062           -117.6                -118.5                -117.5
C5145                0                     0                     0
C5696                0                     0                     0

Output file B.txt

C2062 X -117.6
C2062 Y -118.5
C2062 Z -117.5
C5145 X O
C5145 Y 0
C5145 Z 0
C5696 X 0
C5696 Y 0
C5696 Z 0

Easy (but you have to add a condition if you want to manage lines with only one field):

awk '{for (i=2;i<=NF;i++) {print $1 OFS $i}}' A.txt > B.txt
1 Like

how can i get X Y Z in second column ?

try this not able to run, hope it gives output :slight_smile:

awk '{ for(i=1;i<=3;i++){if(i==1){print $1,"X",$2}else {if (i==2) { print $1,"Y",$3} else{print $1,"Z",$4}}}}' file
1 Like
awk -v f2='X Y Z' 'BEGIN{split(f2,f2a)} {for(i=1;i<NF;i++) print $1,f2a, $(i+1)}' myFile
1 Like