我有一个空格分隔的原始文本文件,我想生成一个excel文件。下面是一个非常小的格式样本:
Name1
Name1 Field1 Value1a
Name1 Field2 Value2a
Name1 Field3 Value3a
Name2
Name2 Field1 Value2a
Name2 Field2 Value2a
Name2 Field3 Value3a
Name2 Field4 Value4a
Name2 Field5 Value5a
Name2 Field6 Value6a
Name2 Field7 Value7a
.
.
.
Name N
NameN Field1 Value1c
NameN Field2 Value2b
NameN Field3 Value3a
NameN Field8 Value8a
NameN Field6 Value6d
NameN Field7 Value7a
我想要做的是删除"空白"行并使用脚本以下列格式重新组织。
Field1 Field2 Field3 Field4 Field5 Field6 Field7 Field8
Name1 Value1a Value2a Value3a
Name2 Value2a Value2a Value3a Value4a Value5a Value6a Value7a
.
.
.
NameN Value1c Value2b Value3a Value6d Value7a Value8a
我已经提到了很多关于实现这一目标的信息,但无法弄清楚这最后一部分。任何帮助将不胜感激!
答案 0 :(得分:0)
使用CSV文件,Excel可以原生打开。
假设:您的输入数据不包含空格或制表符,但字段分隔符除外
gawk '
NF < 3 {next}
!($1 in names) {names[$1]}
!($2 in fields) {fields[$2]}
{data[$1,$2] = $3}
END {
PROCINFO["sorted_in"] = "@ind_str_asc"
for (f in fields) printf ",%s", f
print ""
for (n in names) {
printf "%s", n
for (f in fields) {
printf ",%s", (data[n,f] == "") ? "\"\"" : data[n,f]
}
print ""
}
}
' file > file.csv
cat file.csv
,Field1,Field2,Field3,Field4,Field5,Field6,Field7,Field8
Name1,Value1a,Value2a,Value3a,"","","","",""
Name2,Value2a,Value2a,Value3a,Value4a,Value5a,Value6a,Value7a,""
NameN,Value1c,Value2b,Value3a,"","",Value6d,Value7a,Value8a