4

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, since it can only insert a character as delimiter. If first file has lines with different lengthes output will be twisted:

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

If it was a command for adding 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: hypothetical commands for adding trailing spaces exist (for example xargs -d '\n' printf '%-50s\n')

But it is worth to notice that solution

$ paste <(adding-spaces-command file1) file2

will not work as expected when file1 contain lesser amount of lines than file2 does.

5

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. Your 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
    
    line
    

    Input file2:

    hello
    world
    

    Output:

    looooooooong line  hello
                       world
    line
    
  • Generally paste -d @ file1 file2 | column -t -n -s @ where @ is any delimiter that is not contained in files. – belkka Sep 16 '17 at 18:46
  • @belkka exactly – αғsнιη Jul 28 '18 at 18:02
8

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
1

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
                               hello
                               world
                               hello
                               world

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

hello                          looooooooong line
world                          line
hello
world
hello
world
1

Try:

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.