【问题标题】:Data partitioning by columns按列进行数据分区
【发布时间】:2013-07-22 05:13:39
【问题描述】:

我有一个 50 行和 150 万列的大矩阵。在这 150 万列中,前两个是我的标题。

我正在尝试将我的数据按列分成小块。因此,例如每个小集合将是 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
...

但以上都不起作用。

有没有一种有效的方法来做到这一点?

【问题讨论】:

  • 什么软件在它的正常思维中会输出 150 万列(你的意思是 M 表示百万?还是 M 表示罗马数字表示 1000?)(无论哪种方式都很疯狂,只是数量级不同;-) )。你不能让数据以相反的方式传递,50 列,150 万行吗?祝你好运!

标签: awk cut data-partitioning


【解决方案1】:

的一种方式。我不知道它(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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 2017-07-05
    • 2016-10-15
    • 2022-01-23
    • 2021-08-10
    • 2022-07-29
    • 2015-05-15
    相关资源
    最近更新 更多