Display two files side by side

2019-01-16 03:50发布

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

Given one.txt and 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.

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

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)?

9条回答
甜甜的少女心
2楼-- · 2019-01-16 04:03

There is a sed way:

f1width=$(wc -L <one.txt)
f1blank="$(printf "%${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!):

查看更多
倾城 Initia
3楼-- · 2019-01-16 04:04

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 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:

查看更多
欢心
4楼-- · 2019-01-16 04:05
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
查看更多
三岁会撩人
5楼-- · 2019-01-16 04:07

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.

paste one.txt two.txt |awk -F'\t' '{printf("%-50s %s\n",$1,$2)}'
查看更多
地球回转人心会变
6楼-- · 2019-01-16 04:08

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:

diff -y -W 150 file1.cf file2.cf
查看更多
冷血范
7楼-- · 2019-01-16 04:10

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

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
查看更多
登录 后发表回答