Text Processing – Combine Text Files Column-Wise

tabletext processing

I have two text files. The first one has content:

Languages
Recursively enumerable
Regular

while the second one has content:

Minimal automaton
Turing machine
Finite

I want to combine them into one file column-wise. So I tried paste 1 2 and its output is:

Languages   Minimal automaton
Recursively enumerable  Turing machine
Regular Finite

However I would like to have the columns aligned well such as

Languages               Minimal automaton
Recursively enumerable  Turing machine
Regular                 Finite

I was wondering if it would be possible to achieve that without manually handling?


Added:

Here is another example, where Bruce method almost nails it, except some slight misalignment about which I wonder why?

$ cat 1
Chomsky hierarchy
Type-0
—

$ cat 2
Grammars
Unrestricted

$ paste 1 2 | pr -t -e20
Chomsky hierarchy   Grammars
Type-0              Unrestricted
—                    (no common name)

Best Answer

You just need the column command, and tell it to use tabs to separate columns

paste file1 file2 | column -s $'\t' -t

To address the "empty cell" controversy, we just need the -n option to column:

$ paste <(echo foo; echo; echo barbarbar) <(seq 3) | column -s $'\t' -t
foo        1
2
barbarbar  3

$ paste <(echo foo; echo; echo barbarbar) <(seq 3) | column -s $'\t' -tn
foo        1
           2
barbarbar  3

My column man page indicates -n is a "Debian GNU/Linux extension." My Fedora system does not exhibit the empty cell problem: it appears to be derived from BSD and the man page says "Version 2.23 changed the -s option to be non-greedy"

Related Question