2013-07-21 124 views
0

我有一个50行和1.5M列的大矩阵。从这150M栏中,前两个是我的标题。按列分组数据

我想把我的数据按列分成小块。因此,例如每个小组将是50行和100列。但是每个小数据都必须将上面提到的前两列作为标题。

我试图

awk '{print $1"\t"$2"\t"}' test | cut -f 3-10 
awk '{print $1"\t"$2"\t"}' test | cut -f 11-20 
... 

cut -f 1-2 | cut -f 3-10 test 
cut -f 1-2 | cut -f 11-20 test 
... 

但没有上述工作。

有没有这样做的有效方式?

+0

什么软件在其右侧的脑海里输出1.5M列(你平均值m为百万或M在罗马数字?为1000?)(无论哪种方式它的疯狂,只是不同的数量级;-))。难道你不能通过另一种方式获得数据:50列,150M行吗?祝你好运! – shellter

回答

0

单程。我不知道它是否(awk)可以处理如此大量的列,但请试一试。它使用模数运算符为每个特定数量的列削减行。

awk '{ 
     ## Print header of first line. 
     printf "%s%s%s%s", $1, FS, $2, FS 
     ## Count number of columns printed, from 0 to 100. 
     count = 0 
     ## Traverse every columns but the first two keys. 
     for (i = 3; i <= NF; i++) { 
      ## Print header again when counted 100 columns. 
      if (count != 0 && count % 100 == 0) { 
       printf "%s%s%s%s%s", ORS, $1, FS, $2, FS 
      } 
      ## Print current column and count it. 
      printf "%s%s", $i, FS 
      ++count 
     } 
     ## Separator between splits. 
     print ORS 
    } 
' infile 

我两条线和4列,而不是100进行了测试。下面是测试文件:

key1 key2 one two three four five six seven eight nine ten 
key1 key2 one2 two2 three2 four2 five2 six2 seven2 eight2 nine2 ten2 

和结果:

key1 key2 one two three four 
key1 key2 five six seven eight 
key1 key2 nine ten 

key1 key2 one2 two2 three2 four2 
key1 key2 five2 six2 seven2 eight2 
key1 key2 nine2 ten2