代码之家  ›  专栏  ›  技术社区  ›  Xophmeister

连接行,对记录数进行模化

  •  2
  • Xophmeister  · 技术社区  · 7 年前

    假设我的流是x*N行长,其中x是记录数,N是每条记录的列数,并且是按列输出的。例如,x=2,N=3:

    1
    2
    Alice
    Bob
    London
    New York
    

    如何将每一行按记录数归并到列中:

    1   Alice   London
    2   Bob     New York
    

    paste ,带N - s、 我得到了转置的输出。我可以用 split -l 粘贴 ,但我想在流中进行,而不必到处吐临时文件。

    join 解决方案,但我看不到。。。


    编辑

    1
    2
    3
    4
    5
    a
    b
    c
    d
    e
    alpha
    beta
    gamma
    delta
    epsilon
    

    1   a   alpha
    2   b   beta
    3   c   gamma
    4   d   delta
    5   e   epsilon
    
    3 回复  |  直到 7 年前
        1
  •  2
  •   glenn jackman    7 年前

    您正在寻找 pr

    pr -T -s$'\t' -3 <<'END_STREAM'
    1
    2
    Alice
    Bob
    London
    New York
    END_STREAM
    
    1       Alice   London
    2       Bob     New York
    

    公共关系 在coreutils中。

        2
  •  1
  •   ghoti    7 年前

    pr 公共关系 int文件。它是 part of POSIX.1

    $ pr -3 -t < inp1
    1                       a                       alpha
    2                       b                       beta
    3                       c                       gamma
    4                       d                       delta
    5                       e                       epsilon
    

    或者如果你愿意,

    $ pr -3 -t -s, < inp1
    1,a,alpha
    2,b,beta
    3,c,gamma
    4,d,delta
    5,e,epsilon
    

    $ pr -3 -t -w 20 < inp1
    1      a      alpha
    2      b      beta
    3      c      gamma
    4      d      delta
    5      e      epsilo
    

    man pr 查看操作系统中的特定选项。

        3
  •  0
  •   hek2mgl    7 年前

    为了可靠地处理输入,您需要知道输出文件中的列数或输出文件中的行数。如果只知道列数,则需要读取输入文件两次。

    # If you don't know the number of output lines but the
    # number of output columns in advance you can calculate it
    # using wc -l 
    
    # Split the file by the number of output lines
    split -l"${olines}" file FOO # FOO is a prefix. Choose a better one
    paste FOO*
    

    如果您事先知道输出列的数量,可以使用此 awk

    转换awk公司

    BEGIN {
        # Split the file into one big record where fields are separated
        # by newlines
        RS=''
        FS='\n' 
    }
    FNR==NR {
        # We are reading the file twice (see invocation below)
        # When reading it the first time we store the number
        # of fields (lines) in the variable n because we need it
        # when processing the file.
        n=NF
    }
    {
        # n / c is the number of output lines
        # For every output line ...
        for(i=0;i<n/c;i++) {
            # ... print the columns belonging to it
            for(ii=1+i;ii<=NF;ii+=n/c) {
                printf "%s ", $ii
            }
            print "" # Adds a newline
        }
    }
    

    这样称呼:

    awk -vc=3 -f convert.awk file file # Twice the same file
    

    如果您事先知道输出线的数量,可以使用以下方法 脚本:

    :

    BEGIN {
        # Split the file into one big record where fields are separated
        # by newlines
        RS=''
        FS='\n' 
    }
    {
        # x is the number of output lines and has been passed to the 
        # script. For each line in output
        for(i=0;i<x;i++){
            # ... print the columns belonging to it
            for(ii=i+1;ii<=NF;ii+=x){
                printf "%s ",$ii
            }   
            print "" # Adds a newline
        }   
    }
    

    awk -vx=2 -f convert.awk file