2

Sample Input:

id,Product1,Product2,Product3,Product4
1,0.1,0.3,0.8,0.7
2,0.6,0.7,0.5,0.9

I need output as :

id,productname,product_val
1,Product1,0.1
1,Product2,0.3
1,Product3,0.8
1,Product4,0.7
2,Product1,0.6
2.Product2,0.7
3,Product3,0.5

I had tried

awk -F, 'NR==1 { for (i=1; i<=NF; i++) sn[i]=$i }
         NR>1 { for (i=1; i<=NF; i++) print sn[i] "," $i;}' filename.csv
3

except for the new headings, which you can easily add, this does it:

awk -F, 'NR==1 { for (i=2; i<=NF; i++) sn[i]=$i }
     NR>1 { for (i=2; i<=NF; i++) print $1 "," sn[i] "," $i }' filename.csv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.