如何在 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 来做到这一点,但希望输出显示到 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
https://stackoverflow.com/questions/13341832/