根据定界符在多列中打印文件 [英] Print a file in multiple columns based on delimiter

查看:75
本文介绍了根据定界符在多列中打印文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个简单的任务,但是使用duckduckgo却无法找到一种方法来正确地完成我想做的事情.

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.

主要问题是:如何使用定界符将linux或bash中命令的输出拆分为多列?

The main question is: How do I split the output of a command in linux or bash into multiple columns using a delimeter?

我有一个看起来像这样的文件:(这只是一个简化的示例)

I have a file that looks like this: (this is just a simplified example)

-----------------------------------
Some data
that varies in line length
-----------------------------------

-----------------------------------
More data that is seperated
by a new line and dashes
-----------------------------------

以此类推.每次将数据写入文件时,它都会用短划线括起来,并在最后一个块中用空行分隔.数据的线长不同.我想要的基本上是一种使用bash将文件拆分为多个列的工具或方式,如下所示:

And so on. Everytime data gets written to the file, it's enclosed in a line of dashes, seperated by an empty line from the last block. Line-length of the data varies. What I want is basically a tool or way using bash to split the file into multiple columns like this:

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

每列应占据屏幕的50%,不需要居中(对齐时). 文件要按每个块进行拆分.在中间分割文件或类似的操作将不起作用.我基本上想将块1移到左列,将块2移到右边,再将3移到左边,再将4移到右边,依此类推.该文件会不断更新,更新应立即写入屏幕. (当前我正在使用tail -f)

Each column should take 50% of the screen, no centering (as in alignment) needed. The file has to be split per-block. Splitting the file in the middle or something like that won't work. I basically want block 1 go to the left column, block 2 to the right, 3 to the left again, 4 right, and so on. The file gets updated constantly and updates should be written to the screen right away. (Currently I'm using tail -f)

由于这听起来像是一个相当普遍的问题,因此我欢迎采用一般方法,而不是仅针对我的情况的特定答案,因此来自搜索引擎的人们正在寻找一种在bash中采用两列布局的方法,信息也.我尝试了columnpr,都无法按需工作. (我在评论中对此做了详细说明)

Since this sounds like a rather common question I would welcome a general approach to this instead of a specific answer that works only for my case so people coming from search engines looking for a way to have a two column layout in bash get some information too. I tried column and pr, both don't work as desired. (I elaborated on this in the comments)

编辑:为了清楚起见,我正在寻找一种常规方法.遍历一个文件,在定界符之间获取数据,将其放入A列,将下一个放入B列,依此类推.

To be clear, I am looking for a general approach on this. Going through a file, getting data between the delimiter, putting it to column A, getting the next one putting it to column B, and so on.

推荐答案

好的,因为显然没有可行的方法,所以我提出了自己的解决方案.有点混乱,需要安装GNU screen,但是可以工作.块内或块周围的任何数量的行,屏幕的50%会自动调整大小,并且每一列都彼此独立打印,并且它们之间有固定数量的换行符.还每x秒自动更新一次. (在我的示例中为120)

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

首先,使用screen -S testscr启动屏幕会话,然后在会话内或会话外执行上面的脚本.这将以每列50%的比例垂直分割屏幕,并在两列上执行tail -f,然后它将遍历输入文件,并逐块写入每个tmp.以所需的方式归档.由于它处于一个无限的while循环中,因此它基本上每隔x秒自动更新显示的输出(此处为120).

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).

这篇关于根据定界符在多列中打印文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆