linux - 并排显示两个文件

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

给定 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 以进行管道等。

我想出的解决方案是使用 sdiff然后通过管道传送到 sed 以删除输出 sdiff补充道。

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

我可以创建一个函数并将其粘贴到我的 .bashrc 中但肯定已经存在用于此的命令(或者可能是 cleaner 解决方案)?

最佳答案

您可以使用 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

另见:

  • Print command result side by side
  • Combine text files column-wise

https://stackoverflow.com/questions/13341832/

相关文章:

python - 为什么 (1 in [1,0] == True) 评估为 False?

python - 是否可以将已编译的 .pyc 文件反编译为 .py 文件?

python - 将字符串打印为十六进制字节

linux - 如何通过命令行将图像转换为灰度?

c - 如何设置 cron 作业以每小时运行一次可执行文件?

linux - 如何为 bash 命令设置多个环境变量

python - 使用 pip 安装 SciPy 和 NumPy

python - 在python中将整数转换为二进制

c - 如何让 backtrace()/backtrace_symbols() 打印函数名?

python - 使用字典计算列表中的项目