Print a file in multiple columns based on delimiter

前端 未结 4 831
广开言路
广开言路 2021-01-23 08:37

This seems like a simple task, but using duckduckgo I wasn\'t able to find a way to properly do what I\'m trying to.

The main question is: How do I split the output of a

4条回答
  •  礼貌的吻别
    2021-01-23 09:27

    Assuming file contains uniform blocks of five lines each, using paste, sed, and printf:

    c=$((COLUMNS/2)) 
    paste -d'#' <(sed -n 'p;n;p;n;p;n;p;n;p;n;n;n;n;n' file) \
                <(sed -n 'n;n;n;n;n;p;n;p;n;p;n;p;n;p' file) | 
    sed 's/.*/"&"/;s/#/" "/' | 
    xargs -L 1 printf "%-${c}s %-${c}s\n"
    

    Problem with OP spec

    The OP reports that the block lengths may vary, and should be separated by a fixed number of lines. Even numbered blocks go in Column A, odd numbered blocks in Column B.

    That creates a tail -f problem then. Suppose the block lengths of the source input begin with 1000 lines, then one line, 1000, 1, 1000, 1, etc. So Column A gets all the 1000 line blocks, and Column B gets all the one line blocks. Let's say the blocks in the output are separated by 1 line each. So one block in Column A lines up with 500 blocks in Column B. So for a terminal with scrolling output, that means before we can output the first block in Column A, we have to wait for 1000 blocks of input. To output the third block in Column A, (just below the first block), we have to wait for 2000 blocks of input.

    If the blocks are added to the input file relatively slowly, with a one second delay between blocks, then it will take three seconds for the block 3 to appear in the input file, but it will take 33 minutes for block 3 to be displayed in the output file.

提交回复
热议问题