输入示例:
id,产品1,产品2,产品3,产品4 1,0.1,0.3,0.8,0.7 2,0.6,0.7,0.5,0.9
我需要输出为:
id、产品名称、产品值 1,产品1,0.1 1、产品2、0.3 1、产品3、0.8 1,产品4,0.7 2、产品1,0.6 2.产品2,0.7 3、产品3、0.5
我曾尝试过
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
答案1
除了可以轻松添加的新标题之外,这样做是这样的:
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