在两列中打印两个文件

bel*_*kka 6 command-line paste text-formatting

我想在两列中打印两个文件——第一个文件在左侧,第二个文件在右侧。

paste 不做这项工作,因为它只能插入一个字符作为分隔符,所以如果第一个文件行的长度不同,输出将被扭曲:

$ cat file1
looooooooong line
line
$ cat file2
hello
world
$ paste file1 file2
looooooooong line   hello
line    world
Run Code Online (Sandbox Code Playgroud)

如果这是一个添加尾随空格的命令,fmt --add-spaces --width 50问题将得到解决:

$ paste <(fmt --add-spaces --width 50 file1) file2
looooooooong line                                 hello
line                                              world
Run Code Online (Sandbox Code Playgroud)

但我不知道一个简单的方法来做到这一点。

那么如何在不扭曲的情况下水平合并打印多个文件呢?其实,我只是想同时看它们。


UPD:添加尾随空格的命令确实存在(例如,xargs -d '\n' printf '%-50s\n'

但解决方案如

$ paste <(add-trailing-spaces file1) file2
Run Code Online (Sandbox Code Playgroud)

当 file1 的行数少于 file2 时,它不会按预期工作。

Rom*_*est 10

使用单个pr命令:

pr -Tm file[12]
Run Code Online (Sandbox Code Playgroud)
  • -T( --omit-pagination) - 省略页眉和尾页,消除输入文件中设置的表单馈送的任何分页

  • -m( --merge) - 并行打印所有文件,每列一个


αғs*_*нιη 8

怎么样paste file{1,2}| column -s $'\t' -tn

looooooooong line line  hello
line                    world
Run Code Online (Sandbox Code Playgroud)
  • 这是告诉column使用Tab的列的分离器,在我们需要它从paste它是默认的分隔符有如果未指定命令; 一般来说:

    paste -d'X' file{1,2}| column -s $'X' -tn

    whereX表示任何单个字符。您需要选择一个不会出现在您的文件中的授权。

  • -t选项用于确定输入包含的列数。

  • 这不会在两个文件之间添加长标签,而其他答案会。
  • 即使 file1 中有空行,这也能工作,并且不会在 file1 的打印区域打印第二个文件,请参见下面的输入/输出

    输入文件1:

    looooooooong line
    
    line
    
    Run Code Online (Sandbox Code Playgroud)

    输入文件2:

    hello
    world
    
    Run Code Online (Sandbox Code Playgroud)

    输出:

    looooooooong line  hello
                       world
    line
    
    Run Code Online (Sandbox Code Playgroud)