Print a file in multiple columns based on delimiter

前端 未结 4 833
广开言路
广开言路 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:18

    Alright, since apprently there is no clean way to do this I came up with my own solution. It's a bit messy and requires GNU screen to be installed, but it works. Any amount of lines within or around the blocks, 50% of the screen automatically resizing and each column prints independantly from each other with a fixed amount of newlines between them. Also automatic updates every x seconds. (120 in my example)

    #!/bin/bash
    
    screen -S testscr -X layout save default
    screen -S testscr -X split -v
    screen -S testscr -X screen tail -f /tmp/testscr1.txt
    screen -S testscr -X focus
    screen -S testscr -X screen tail -f /tmp/testscr2.txt
    
    while : ; do
        echo "" > /tmp/testscr1.txt
        echo "" > /tmp/testscr2.txt
        cfile=1 # current column
        ctype=0 # start or end of block
    
        while read; do
            if [[ $REPLY == "------------------------------------------------------------" ]]; then
                if [[ $ctype -eq 0 ]]; then
                    ctype=1
                else
                    if [[ $cfile -eq 1 ]]; then
                        echo "${REPLY}" >> /tmp/testscr1.txt
                        echo "" >> /tmp/testscr1.txt
                        echo "" >> /tmp/testscr1.txt
                        cfile=2
                    else
                        echo "${REPLY}" >> /tmp/testscr2.txt
                        echo "" >> /tmp/testscr2.txt
                        echo "" >> /tmp/testscr2.txt
                        cfile=1
                    fi
                    ctype=0
                fi
            fi
            if [[ $ctype -eq 1 ]]; then
                if [[ $cfile -eq 1 ]]; then
                    echo "${REPLY}" >> /tmp/testscr1.txt
                else
                    echo "${REPLY}" >> /tmp/testscr2.txt
                fi
            fi
        done < "$1"
        sleep 120
    done
    

    First, start a screen session with screen -S testscr then, either within or outside the session, execute the script above. This will split the screen vertically using 50% per column and execute tail -f on both columns, afterwards it will go through the input file and write block by block to each tmp. file in the desired way. Since it's in an infinite while loop it's essentially automatically updating the shown output every x seconds (here 120).

提交回复
热议问题