如何在 shell
中并排显示 2 个不同长度的未排序文本文件 (按列)
给定 one.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
展示:
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 进行此操作,但希望将输出显示到标准输出以进行管道等.
我想出的解决方案使用 sdiff
然后管道到 sed 以删除输出 sdiff
添加。
sdiff one.txt two.txt | sed -r 's/[<>|]//;s/(\t){3}//'
我可以创建一个函数并将其粘贴到我的 .bashrc
但肯定已经存在用于此的命令(或者可能是 更清洁 的解决方案)?
原文由 Chris Seymour 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以使用
pr
来执行此操作,使用-m
标志来合并文件,每列一个,并-t
来省略标题,例如。输出:
也可以看看: