简体   繁体   English

并排显示两个文件

[英]Display two files side by side

How can 2 unsorted text files of different lengths be display side by side (in columns) in a shell如何在shell并排(按列)显示 2 个不同长度的未排序文本文件

Given one.txt and two.txt :鉴于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

Display:展示:

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 almost does the trick but doesn't align the columns nicely as it just prints one tab between column 1 and 2. I know how to this with emacs and vim but want the output displayed to stdout for piping ect. paste one.txt two.txt几乎可以解决问题,但不能很好地对齐列,因为它只是在第 1 列和第 2 列之间打印一个选项卡。我知道如何使用 emacs 和 vim 执行此操作,但希望将输出显示到标准输出以进行管道等.

The solution I came up with uses sdiff and then pipes to sed to remove the output sdiff adds.我想出的解决方案使用sdiff然后管道到 sed 以删除输出sdiff添加。

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

I could create a function and stick it in my .bashrc but surely a command for this exists already (or a cleaner solution potentially)?我可以创建一个函数并将其粘贴在我的.bashrc但肯定已经存在用于此的命令(或者可能是更清洁的解决方案)?

You can use pr to do this, using the -m flag to merge the files, one per column, and -t to omit headers, eg.您可以使用pr来执行此操作,使用-m标志来合并文件,每列一个,并使用-t来省略标题,例如。

pr -m -t one.txt two.txt

outputs:输出:

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

                                    skipped a line

See Also:也可以看看:

To expand a bit on @Hasturkun 's answer: by default pr uses only 72 columns for its output, but it's relatively easy to make it use all available columns of your terminal window:@Hasturkun的回答进行一点扩展:默认情况下, pr仅使用 72 列作为其输出,但使其使用终端窗口的所有可用列相对容易:

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

Most shell's will store (and update) your terminal's screenwidth in the $COLUMNS environment variable, so we're just passing that value on to pr to use for its output's width setting.大多数 shell 将在$COLUMNS环境变量中存储(和更新)终端的屏幕宽度,因此我们只是将该值传递给pr以用于其输出的宽度设置。

This also answers @Matt 's question:这也回答了@Matt的问题:

Is there a way for pr to auto-detect screen width? pr 有没有办法自动检测屏幕宽度?

So, no: pr itself can't detect the screenwidth, but we're helping out a bit by passing in the terminal's width via the -w option.所以,不: pr本身无法检测屏幕宽度,但我们通过-w选项传入终端的宽度来提供一些帮助。

If you know the input files have no tabs, then using expand simplifies @oyss 's answer :如果您知道输入文件没有选项卡,那么使用expand简化@oyss回答

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

If there could be tabs in the input files, you can always expand first:如果输入文件中可能有选项卡,您始终可以先展开:

paste <(expand one.txt) <(expand two.txt) | expand --tabs=50
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])}
}'

Using * in a format specification allows you to supply the field length dynamically.在格式规范中使用*允许您动态提供字段长度。

There is a sed way:有一种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 /;
    "

Under , you could use printf -v :,您可以使用printf -v

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

(Of course @Hasturkun 's solution pr is the most accurate !) : (当然@Hasturkun 的解决方案pr最准确的!)

Advantage of sed over pr sed优于pr

You can finely choose separation width and or separators:您可以精细选择分隔宽度和/或分隔符:

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

Or, for sample, to mark lines containing line :或者,对于示例,标记包含line

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

will render:将呈现:

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

从 Barmar 的答案中删除动态字段长度计数将使其成为一个更短的命令......但您仍然需要至少一个脚本来完成无论您选择什么方法都无法避免的工作。

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

If you want to know the actual difference between two files side by side, use diff -y :如果您想并排了解两个文件之间的实际差异,请使用diff -y

diff -y file1.cf file2.cf

You can also set an output width using the -W, --width=NUM option:您还可以使用-W, --width=NUM选项设置输出宽度:

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

and to make diff 's column output fit your current terminal window:并使diff的列输出适合您当前的终端窗口:

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

Find below a python based solution.在下面找到一个基于 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]

Example例子

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

                                 skipped a line
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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM