Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display two files side by side

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

like image 410
Chris Seymour Avatar asked Nov 12 '12 10:11

Chris Seymour


People also ask

How can I use two files at the same time?

To select multiple files on Windows 10 from a folder, use the Shift key and select the first and last file at the ends of the entire range you want to select. To select multiple files on Windows 10 from your desktop, hold down the Ctrl key as you click on each file until all are selected.

How do you split screen windows?

There is a shortcut to split windows that's really useful once you get used to the process: Press down the Windows logo key while in an active window, and then press either the left or right arrow key. This should automatically assign a side of the screen and split the window over there.


2 Answers

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
like image 154
Hasturkun Avatar answered Oct 28 '22 14:10

Hasturkun


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.

like image 41
pvandenberk Avatar answered Oct 28 '22 16:10

pvandenberk