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)?
There is a
sed
way:(Of course @Hasturkun 's solution
pr
is the most accurate!):You can use
pr
to do this, using the-m
flag to merge the files, one per column, and-t
to omit headers, eg.outputs:
See Also:
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.
If you want to know the actual difference between of two files use below command
you can also set width to print columns using the
-W, --width=NUM
option:If you know the input files have no tabs, then using
expand
simplifies @oyss's answer:If there could be tabs in the input files, you can always expand first: