如何使用unix命令将文本文件中的列打印到行?

时间:2010-07-21 12:15:55

标签: linux unit-testing unix awk nawk

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

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

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

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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中做类似的事情,如果你比我更容易接受它。

答案 2 :(得分:0)

基本上这个想法是我想你需要转置数据。 stackoverflow has a similar question with a very good solution

只剩下任务是你需要使用你的脚本技能

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

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

我猜这个解决方案的一半已经提供,您可以通过简单的脚本管理剩余的解决方案。