I want to print two files in two columns -- first file on the left side and second on the right side.

paste doesn't do the job, because it can only insert a character as delimiter, so if first file lines have different length output will be twisted:

$ cat file1
looooooooong line
$ cat file2
$ paste file1 file2
looooooooong line   hello
line    world

If it was a command to add trailing spaces like fmt --add-spaces --width 50 the problem would be solved:

$ paste <(fmt --add-spaces --width 50 file1) file2
looooooooong line                                 hello
line                                              world

But I don't know a simple way to do this.

So how to merge and print several files horizontally without twisting? Actually, I just want to look at them simultaneously.

UPD: command to add trailing spaces does exist (for example, xargs -d '\n' printf '%-50s\n')

But solution like

$ paste <(add-trailing-spaces file1) file2

does not work as expected when file1 has fewer lines than file2.


What about paste file{1,2}| column -s $'\t' -tn?

looooooooong line line  hello
line                    world
  • This is telling column to use Tab as columns' separator where we takes it from the paste command which is the default seperator there if not specified; generally:

    paste -d'X' file{1,2}| column -s $'X' -tn

    where X means any single character. You need to choose the one which granted that won't be occur in your files.

  • The -t option is used to determine the number of columns the input contains.

  • This will not add long tab between two files while other answers does.
  • this will work even if there was empty line(s) in file1 and it will not print second file in print area of file1, see below input/ouput

    Input file1:

    looooooooong line

    Input file2:



    looooooooong line  hello

With single pr command:

pr -Tm file[12]
  • -T (--omit-pagination) - omit page headers and trailers, eliminate any pagination by form feeds set in input files

  • -m (--merge) - print all files in parallel, one in each column

  • What does the -T do?
    – Kusalananda
    Sep 16 '17 at 18:37
  • This can truncate long lines without any warning, but I like it.
    – belkka
    Sep 16 '17 at 19:54
  • On my mac I had to use pr -tm file[12]. (minus -t)
    – Ulysse BN
    Oct 15 '19 at 9:37

A portable solution:

$ paste file1 file2 | awk -F'\t' '{ printf("%-30s %s\n", $1, $2) }'
looooooooong line              hello
line                           world

This uses paste to produce a tab-delimited input for awk.

The awk script simply takes the two tab-delimited fields and outputs them using printf(). A column of 30 characters is reserved for the first file. The %-30s means "30 positions of string data with left alignment". Removing the - would produce a column aligned to the right, and changing 30 would change the column width.

It also deals with uneven length files. Here I've added lines to the second file:

looooooooong line              hello
line                           world

And, when reversing the order of the files on the command line:

hello                          looooooooong line
world                          line


paste -d '\n' file1 file2 | xargs -d '\n' printf '%-30s  %-30s\n'

Inspired by @Kusalananda's solution.

Note: The -d parameter of xargs is only available on GNU version, but not on BSD.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.