我有一个50行,1.5M列的大矩阵。在这1.5米的列中,前两列是我的标题。
我正在尝试将我的数据按列划分为小块。因此,例如,每个小集合将是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
...
但上述方法都不起作用。
有没有一种有效的方法来做到这一点?
发布于 2013-07-22 05:13:39
使用awk的一种方式。我不知道它(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
https://stackoverflow.com/questions/17776515
复制相似问题