Display two files side by side
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
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:
- Print command result side by side
- Combine text files column-wise
To expand a bit on @Hasturkun's answer: by default pr
uses only 72 columns for its output, but it's relatively easy to make it use all available columns of your terminal window:
pr -w $COLUMNS -m -t one.txt two.txt
Most shells will store (and update) your terminal's screenwidth in the $COLUMNS
shell variable, so we're just passing that value on to pr
to use for its output's width setting.
This also answers @Matt's question:
Is there a way for pr to auto-detect screen width?
So, no: pr
itself can't detect the screenwidth, but we're helping it out a bit by passing in the terminal's width via its -w
option.
Note that $COLUMNS
is a shell variable, not an environment variable, so it isn't exported to child processes, and hence the above approach will likely not work in scripts, only in interactive TTYs... see LINES and COLUMNS environmental variables lost in a script for alternative approaches.