热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

并排显示两个文件-Displaytwofilessidebyside

Howcan2unsortedtextfilesofdifferentlengthsbedisplaysidebyside(incolumns)inashell

How can 2 unsorted text files of different lengths be display side by side (in columns) in a shell

如何在shell中并排显示两个不同长度的未排序文本文件(以列表示)

Given one.txt and two.txt:

给一个。txt和two.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.

粘贴一个。三两个。txt几乎可以做到这一点,但它并没有很好地对齐列,因为它只是在列1和列2之间打印一个选项卡。我知道如何使用emacs和vim来实现这一点,但我希望输出显示为stdout以便进行管道优化。

The solution I came up with uses sdiff and then pipes to sed to remove the output sdiff adds.

我提出的解决方案使用sdiff,然后通过管道将输出sdiff添加到sed,以删除输出sdiff。

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

sdiff。三两个。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中,但是对于它来说,一个命令肯定已经存在(或者可能是一个更干净的解决方案)?

9 个解决方案

#1


127  

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来省略header。

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

#2


22  

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选项通过终端的宽度来帮助我们。

#3


6  

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.

在格式规范中使用*可以动态地提供字段长度。

#4


4  

If you know the input files have no tabs, then using expand simplifies @oyss's answer:

如果您知道输入文件没有制表符,那么使用展开将简化@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

#5


2  

remove dynamically field length counting from Barmar's answer will make it a much shorter command....but you still need at least one script to finish the work which could not be avoided no matter what method you choose.

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

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

#6


1  

There is a sed way:

有一种sed方式:

f1width=$(wc -L 

(Of course @Hasturkun 's solution pr is the most accurate!):

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

#7


0  

diff -y  


[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

#8


0  

If you want to know the actual difference between of two files use below command

如果您想知道两个文件之间的实际区别,请使用下面的命令

diff -y file1.cf file2.cf

you can also set width to print columns using the -W, --width=NUM option:

您还可以使用-W、-width=NUM选项设置打印列的宽度:

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

#9


0  

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

推荐阅读
author-avatar
凹凸曼00威威_694
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有