1

我有这样的文件..例如:

number,dac,amountdac,expdate,0
1111,1,0.000000,2010-07-21,0
1111,2,0.000000,2010-07-21,0
1111,3,0.000000,2010-07-21,0
1111,4,0.000000,2010-07-21,0
1111,5,0.000000,2010-07-21,0
1111,6,0.000000,2010-07-21,0
1111,7,0.000000,2010-07-21,0
1111,8,0.000000,2010-07-21,0
1111,9,0.000000,2010-07-21,0
1111,10,0.000000,2010-07-21,0
2222,1,50.000000,2010-07-21,0
2222,2,0.000000,2010-07-21,0
2222,3,0.000000,2010-07-21,0
2222,4,0.000000,2010-07-21,0
2222,5,0.000000,2010-07-21,0
2222,6,0.000000,2010-07-21,0
2222,7,0.000000,2010-07-21,0
2222,8,10.000000,2010-07-21,0
2222,9,0.000000,2010-07-21,0
2222,10,0.000000,2010-07-21,0
3333,1,0.000000,2010-07-21,0
3333,2,0.000000,2010-07-21,0
3333,3,0.000000,2010-07-21,0
3333,4,0.000000,2010-07-21,0
3333,5,0.000000,2010-07-21,0
3333,6,0.000000,2010-07-21,0
3333,7,0.000000,2010-07-21,0
3333,8,0.000000,2010-07-21,0
3333,9,200.000000,2010-07-21,0
3333,10,50.000000,2010-07-21,0

我想要这样的输出,所有 dac1 到 dac10 的第 1 列编号都是相同的。我给你的标题供你参考。在原始文件中我没有标题。

number,dac1,dac2,dac3,dac4,dac5,dac6,dac7,dac8,dac9,dac10,amountdac1,amountdac2,amountdac3,,amountdac4,amountdac5,amountdac6,amountdac7,amountdac8,amountdac9,,amountdac10,expdate1,expdate2,expdate3,expdate4,expdate5,expdate6,expdate7,expdate8,expdate9,expdate10,0
1111,1,2,3,4,5,6,7,8,9,10,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,2010-07-21,2010-07-21,2010-07-21,2010-07-21,2010-07-21,2010-07-21,2010-07-21,2010-07-21,2010-07-21,2010-07-21,0
2222,1,2,3,4,5,6,7,8,9,10,50.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,10.000000,0.000000,0.000000,2010-07-21,2010-07-21,2010-07-21,2010-07-21,2010-07-21,2010-07-21,2010-07-21,2010-07-21,2010-07-21,2010-07-21,0
3333,1,2,3,4,5,6,7,8,9,10,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,200.000000,50.000000,2010-07-21,2010-07-21,2010-07-21,2010-07-21,2010-07-21,2010-07-21,2010-07-21,2010-07-21,2010-07-21,2010-07-21,0
4

3 回答 3

2
awk -F"," '{ 
   a[$1];
   b[$1]=b[$1]","$2 
   c[$1]=c[$1]","$3
   d[$1]=d[$1]","$4
   e[$1]=e[$1]","$5 }
END{ for(i in a){ print i,b[i],c[i],d[i],e[i] } } ' file
于 2010-07-21T13:03:40.830 回答
0

基本上这个想法是我想你需要转置数据。 stackoverflow 有一个类似的问题,有一个很好的解决方案

剩下的唯一任务是你需要使用你的脚本技能来

  • 获取数据块,即一次 10 行。
  • 删除那 10 行中的第一列并转置数据
  • 添加第一列值(此处为 1111 或 2222 或 3333)

对于输入文件中的所有行,上述所有 3 个步骤都应递归完成。

我想提供了一半的解决方案,您可以通过简单的脚本在此处管理剩余的解决方案。

于 2010-07-21T12:37:26.843 回答
0

您可以编写一个 python 脚本来打破它:

numbers = []
dacs = []
amountdacs = []
expdates = []
for row in text:
    number, dac, amountdac, expdate, zero = row.split(',')
    numbers.append(number)
    dacs.append(dac)
    amountdacs.append(amountdac)
    expdates.append(expdate)
# print things out however you want them

如果你比我更容易使用 perl,你可能会在 perl 中做类似的事情。

于 2010-07-21T12:28:22.667 回答