Merge columns

Hi all - I have a file like below:

A: A1,A2,A3,A4
B: 1,2,3,4
C: z,y,x,w

....

This format repeats

The output should come in a single line merging the first line field with the other two rows:

A1_1 A1_z A2_2 A2_y A3_3 A3_x A4_4 A4_w

Could anyone help with some directions

thanks

cut -d' ' -f2 INPUTFILE | awk -F, '
NR % 3 == 1 { for (i=1; i<=NF; i++) a = $i }
NR % 3 == 2 { for (i=1; i<=NF; i++) b = $i }
NR % 3 == 0 { 
  for (i=1; i<=NF; i++)
    printf a "_" b " " a "_" $i " "
  print ""
}'
A1_1 A1_z A2_2 A2_y A3_3 A3_x A4_4 A4_w 
1 Like

Many thanks.. I guess I can workout the other bits myself :slight_smile:

Great stuff! Could you please explain what the NR % 3 ... lines do?

They separate n+1, n+2, n+3 lines in files. % is modulus operator, n%m gives numbers from 0 to m-1 in a circle - from 0 to 2 here. For example "NR % 3 == 1" means every first, forth, seventh and so on lines, etc.

Super! Got it. Thanks