Transpose multipe columns to rows and adding headers

Hi,

I found the following awk script to transpose multiple (3) columns to multiple rows:

#===
BEGIN {FS=","}

{
for (i=1;i<=NF;i++)
{
 arr[NR,i]=$i;
 if(nf<= NF)
  nf=NF;
 }
nr=NR
}
 
END {
  for(i=1;i<=nf;i++)
   {
    for(j=1;j<=nr;j++)
    {
     printf("%s\t",arr[j,i]);
    }
    printf("\n");
   }
}
#===
 

Although it works well, how could I introduce headers on the output file? I mean (using the "=" and "," as separator field):

2.8 0 9
2.2 0 8
2.1 0 7

to

first=2.8,2.2,2.1
second=0,0,0
third=9,8,7

Thanks in advance

#===
BEGIN {FS=","; split("first,second,third", nameA, FS)}

{
for (i=1;i<=NF;i++)
{
 arr[NR,i]=$i;
 if(nf<= NF)
  nf=NF;
 }
nr=NR
}
 
END {
  for(i=1;i<=nf;i++)
   {
    printf("%s", ((i in nameA)?nameA:"unknown") "=")
    for(j=1;j<=nr;j++)
    {
     printf("%s\t",arr[j,i]);
    }
    printf("\n");
   }
}
#===

Thanks but doesn't work, I get:

first=2.8 0 9 2.2 0 8 2.1 0 7

Instead of:

first=2.8,2.2,2.1
second=0,0,0
third=9,8,7

Could you help me again please?

EGIN {FS=OFS=","; split("first,second,third", nameA, FS)}

{
for (i=1;i<=NF;i++)
{
 arr[NR,i]=$i;
 if(nf<= NF)
  nf=NF;
 }
nr=NR
}

END {
  for(i=1;i<=nf;i++)
   {
    printf("%s", ((i in nameA)?nameA:"unknown") "=")
    for(j=1;j<=nr;j++)
       printf("%s%c",arr[j,i], (j==nr)?ORS:OFS);
   }
}

Very close now:

unknown=2.8 2.2 2.1
unknown=0 0 0
unknown=9 8 7

The "," in between the numbers is needed please. Should it be "BEGIN" instead of "EGIN" right? if so, it looks like this:

first=2.8 0 9,2.2 0 8,2.1 0 7

The former is better.

Please, you support is very appreciated. Thanks.

given gery.txt:

2.8,2.2,2.1
0,0,0
9,8,7

and gery.awk:

BEGIN {FS=OFS=","; split("first,second,third", nameA, FS)}

{
for (i=1;i<=NF;i++)
{
 arr[NR,i]=$i;
 if(nf<= NF)
  nf=NF;
 }
nr=NR
}

END {
  for(i=1;i<=nf;i++)
   {
    printf("%s", ((i in nameA)?nameA:"unknown") "=")
    for(j=1;j<=nr;j++)
       printf("%s%c",arr[j,i], (j==nr)?ORS:OFS);
   }
}
nawk -f gery.awk gery.txt

produces - as expected?:

first=2.8,0,9
second=2.2,0,8
third=2.1,0,7

Ohhh!, sorry for this mistake, the columns are tabulated by space, no comma-tabulated values, it's in the way around:

Input file (gery.txt):
2.8 0 9
2.2 0 8
2.1 0 7

Output file: (comma-tabulated value in rows plus header)
first=2.8,2.2,2.1
second=0,0,0
third=9,8,7

Sorry again for my mistake. Thanks for your great support.

Change the BEGIN block to:

BEGIN {OFS=","; split("first,second,third", nameA, OFS)}

P.S. you ARE welcome!

Many thanks!!! it works like a charm.:b: