Print a file in multiple columns based on delimiter

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

    This script is getting max width of current terminal and splitting it in 2, then printing records split by RS="\n\n" separator, print the first found and placing the cursor at the first line/last column of it to write the next record.

    #!/bin/bash
    
    tput clear
    # get half current terminal width
    twidth=$(($(tput cols)/2))
    
    tail -n 100 -f test.txt | stdbuf -i0 -o0 gawk -v twidth=$twidth 'BEGIN{ RS="\n\n"; FS=OFS="\n"; oldNF=0 } {
        sep="-----------------------------------"
        pad="                    "
        printf "%-" twidth "s", $0
    
        getline
    
        for(i = 1; i <= NF; i++){
        # move cursor to first line, last column of previous record
        print "\033[" oldNF ";" twidth "f" $i
        oldNF+=1
        }
    }'
    

    Here's a simpler version

    gawk 'BEGIN{ RS="[-]+\n\n"; FS="\n" } {
        sep="-----------------------------------"
        le=$2
        lo=$3
        getline
    
        printf "%-40s %-40s\n", sep,sep
        printf "%-40s %-40s\n", le,$2
        printf "%-40s %-40s\n", lo,$3
        printf "%-40s %-40s\n\n", sep,sep
    }' test.txt
    

    Output

    -----------------------------------      -----------------------------------     
    Some data                                More data that is seperated             
    that varies in line length               by a new line and dashes                
    -----------------------------------      -----------------------------------     
    
    -----------------------------------      -----------------------------------     
    Some data                                More data that is seperated             
    that varies in line length               by a new line and dashes                
    -----------------------------------      ----------------------------------- 
    

提交回复
热议问题