【问题标题】:Put every X rows of input into a new column将每 X 行输入放入一个新列
【发布时间】:2016-07-05 11:58:35
【问题描述】:

我有一个包含 3972192 行的文件,每行分隔两个值选项卡。我想将每 47288 行分成一个新列(这来自 84 列)。我阅读了这些其他问题 (Put every N rows of input into a new column),其中它的作用与我想要的相同,但使用 awk 我得到了:

awk: program limit exceeded: maximum number of fields size=32767

如果我用 pr 来做,分隔列的限制是 36。

为此,我首先使用 awk 选择了第 2 列:

awk '{print $2}' input_file>values_file

为了获取我所做的第一列值:

awk '{print $1}' input_file>headers_file

head -n 47288 headers_file >headers_file2

获得这两个文件后,我会将它们与粘贴功能放在一起:

paste -d values_file headers_file2 >Desired_output

示例: 输入:

 -Line1:        ABCD     12

 -Line2:         ASDF     3435

...


-Line47288:     QWER     345466

-Line47289:     ABCD     456

...


-Line94576:     QWER     25

...

-Line3972192    QWER     436

想要的输出:

- Line1:         ABCD     12         456 ....

...

- Line47288:     QWER     345466     25  ....     436

有什么建议吗?提前致谢,

【问题讨论】:

  • 你用 gawk 试过了吗?
  • 请解释一下您是如何计算 84 列的数量的。
  • 我知道有 84 列,因为这是分析 84 个样本的步骤。

标签: linux bash shell awk


【解决方案1】:

我想每个块都有相同的模式,我的意思是,第一列的顺序相同 [ABCD ASDF ... QWER] 并再次。 如果是这样,您必须获取第一个 BLOCK [47288 行] 的第一列并回显到目标文件。 然后您必须获取每个 BLOCK 的第二列并将其粘贴到目标文件中。 我试过这个数据文件:

ABCD 1001 EFGH 1002 IJKL 1003 MNOP 1004 QRST 1005 紫外线WX 1006 ABCD 2001 EFGH 2002 IJKL 2003 MNOP 2004 2005 年第一季度 UVWX 2006 ABCD 3001 EFGH 3002 IJKL 3003 MNOP 3004 QRST 3005 UVWX 3006 ABCD 4001 EFGH 4002 IJKL 4003 MNOP 4004 QRST 4005 UVWX 4006 ABCD 5001 EFGH 5002 IJKL 5003 MNOP 5004 QRST 5005 UVWX 5006

还有这个脚本:


    #!/bin/bash

    #target number of lines, change to 47288
    LINES=6
    INPUT='data.txt'
    TOTALLINES=`wc --lines $INPUT | cut --delimiter=" " --field=1`
    TOTALBLOCKS=$((TOTALLINES / LINES))


    #getting first block of target file, the first column of first LINES of data file
    head -n $LINES $INPUT | cut --field=1 > target.txt

    #get second column of each line, by blocks, and paste it into target file
    BLOCK=1
    while [ $BLOCK -le $TOTALBLOCKS ]
    do
        HEADVALUE=$((BLOCK * LINES))
        head -n $HEADVALUE $INPUT | tail -n $LINES | cut --field=2 > tmpcol.txt
        cp target.txt targettmp.txt
        paste targettmp.txt tmpcol.txt > target.txt
        BLOCK=$((BLOCK+1))
    done

    #removing temp files
    rm -f targettmp.txt
    rm -f tmpcol.txt

我得到了这个目标文件:

ABCD 1001 2001 3001 4001 5001 EFGH 1002 2002 3002 4002 5002 IJKL 1003 2003 3003 4003 5003 MNOP 1004 2004 3004 4004 5004 QRST 1005 2005 3005 4005 5005 UVWX 1006 2006 3006 4006 5006

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 2022-11-23
    • 2015-10-26
    • 1970-01-01
    • 2014-02-08
    相关资源
    最近更新 更多