显示两个文件并排(Display two files side by side)

2019-07-03 18:47发布

不同长度的2个未排序文本文件如何可以是由侧显示侧(列)在一个shell

由于one.txttwo.txt

$ cat one.txt
apple
pear
longer line than the last two
last line

$ cat two.txt
The quick brown fox..
foo
bar 
linux

skipped a line

显示:

apple                               The quick brown fox..
pear                                foo
longer line than the last two       bar 
last line                           linux

                                    skipped a line

paste one.txt two.txt几乎是卓有成效的,但很好,因为它只是打印列1和2之间的一个标签,我知道如何将它与Emacs和Vim,但希望不对齐列输出显示到stdout的管道ECT 。

我想出了利用该解决方案sdiff然后通过管道向sed将删除输出sdiff补充道。

sdiff one.txt two.txt | sed -r 's/[<>|]//;s/(\t){3}//'

我可以创建一个函数,它粘在我.bashrc但肯定这个命令(可能或清洁的解决方案)已经存在?

Answer 1:

您可以使用pr做到这一点,使用-m标志合并文件,每列一个,而-t省略头,例如。

pr -m -t one.txt two.txt

输出:

apple                               The quick brown fox..
pear                                foo
longer line than the last two       bar
last line                           linux

                                    skipped a line

也可以看看:

  • 并排打印命令结果侧
  • 合并文本文件逐列


Answer 2:

为了扩大对位@Hasturkun的回答是:在默认情况下pr只使用72为它的输出列,但它是相对容易使其使用终端窗口中的所有可用列:

pr -w $COLUMNS -m -t one.txt two.txt

大多数shell的将存储(和更新)终端的在屏幕宽度$COLUMNS环境变量,所以我们只是传递到该值pr用于其输出的宽度设置。

这也回答了@马特的问题:

是否有PR自动检测屏幕宽度的方法吗?

所以,不, pr本身无法检测到屏幕宽度,但我们通过通过终端的宽度传球帮助了一下-w选项。



Answer 3:

paste one.txt two.txt | awk -F'\t' '{
    if (length($1)>max1) {max1=length($1)};
    col1[NR] = $1; col2[NR] = $2 }
    END {for (i = 1; i<=NR; i++) {printf ("%-*s     %s\n", max1, col1[i], col2[i])}
}'

使用*的格式规范允许你动态地提供字段长度。



Answer 4:

如果您知道输入文件没有选项卡,然后使用expand简化@oyss的答案 :

paste one.txt two.txt | expand --tabs=50

如果有可能是在输入文件的制表符,你总是可以先展开:

paste <(expand one.txt) <(expand two.txt) | expand --tabs=50


Answer 5:

动态移除Barmar的回答字段长度计将使它更短的命令....但你仍然需要至少一个脚本来完成它不能不管你选择什么方法避免了工作。

paste one.txt two.txt |awk -F'\t' '{printf("%-50s %s\n",$1,$2)}'


Answer 6:

如果你想知道两个文件并排之间的实际差异,利用diff -y

diff -y file1.cf file2.cf

还可以设置使用的输出宽度-W, --width=NUM选项:

diff -y -W 150 file1.cf file2.cf

并提出diff的列输出满足当前终端窗口:

diff -y -W $COLUMNS file1.cf file2.cf


Answer 7:

有一个sed方式:

f1width=$(wc -L <one.txt)
f1blank="$(printf "%${f1width}s" "")"
paste one.txt two.txt |
    sed "
        s/^\(.*\)\t/\1$f1blank\t/;
        s/^\(.\{$f1width\}\) *\t/\1 /;
    "

(当然@Hasturkun的解决方案pr最准确的!):



Answer 8:

diff -y <file1> <file2>


[root /]# cat /one.txt
apple
pear
longer line than the last two
last line
[root /]# cat /two.txt
The quick brown fox..
foo
bar
linux
[root@RHEL6-64 /]# diff -y one.txt two.txt
apple                                                         | The quick brown fox..
pear                                                          | foo
longer line than the last two                                 | bar
last line                                                     | linux


Answer 9:

下面为一个基于Python的解决方案。

import sys

# Specify the number of spaces between the columns
S = 4

# Read the first file
l0 = open( sys.argv[1] ).read().split('\n')

# Read the second file
l1 = open( sys.argv[2] ).read().split('\n')

# Find the length of the longest line of the first file
n = len(max(l0, key=len))

# Print the lines
for i in  xrange( max( len(l0), len(l1) ) ):

    try:
        print l0[i] + ' '*( n - len(l0[i]) + S) + l1[i]
    except:
        try:
            print ' ' + ' '*( n - 1 + S) + l1[i]
        except:
            print l0[i]

apple                            The quick brown fox..
pear                             foo
longer line than the last two    bar 
last line                        linux

                                 skipped a line


文章来源: Display two files side by side