Categories
Development Linux

Transpose columns into rows with first column data repeating using awk

I’ve the input in test.txt as

month,var1,var2
June,10,a
Sep,20,b
Dec,30,c

I want the output as

month,variable,value
June,var1,10
June,var2,a
Sep,var1,20
Sep,var2,b
Dec,var1,30
Dec,var2,c

Note that the number of columns starting from second column is dynamic. It can have 2 columns or 100 columns. How can we transpose the data using awk?

Leave a Reply

Your email address will not be published. Required fields are marked *