使用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