Ramesh
Ramesh

Reputation: 135

Bash paste command output formatting

File1:

1
2

File2:

1 2 3
4 5

File3:

x x x
yy yy
zz

paste file1 file2 file2 gives me a tab separated output:

1       1 2 3   x x x
2       4 5     yy yy
                zz

paste -d" " file1 file2 file3 gives me the output:

1 1 2 3 x x x
2 4 5 yy yy
  zz

I want it like below:

1 1 2 3 x x x
2 4 5   yy yy
        zz

Any idea if this is possible or should I try any other command?

Upvotes: 13

Views: 33417

Answers (4)

fedorqui
fedorqui

Reputation: 289525

paste twice makes it to me:

$ paste <(paste -d" " f1 f2) f3
1 1 2 3 x x x
2 4 5   yy yy
        zz

Upvotes: 4

user3442743
user3442743

Reputation:

Could use sed after to remove tabs

 paste file file2 file3 | sed 's/\t/ /'

 1 1 2 3 x x x
 2 4 5   yy yy
         zz

Here is a general purpose awk script that will work on any number of file with any formatting.

awk '
    {x=ARGIND;a[x]=a[x]>(b=length($0))?a[x]:b}
    {F[FNR,x]=$0}
    END{
            for(q=1;q<=FNR;q++)
            {
                    for(i=1;i<=ARGC;i++)
                    {
                    printf( "%-"a[i]"s ",F[q,i])
                    }print ""
            }
    }' file{1,2,3,4)

Upvotes: 8

Etan Reisner
Etan Reisner

Reputation: 80921

Is this the sort of thing you are looking for?

$ more file{1,2,3,4} | cat
::::::::::::::
file1
::::::::::::::
1
2
::::::::::::::
file2
::::::::::::::
1 2 3
4 5 6 7 8
::::::::::::::
file3
::::::::::::::
x x x
yy yy
zz
::::::::::::::
file4
::::::::::::::
a a a
bb bb bb
c c cc
d d d
$ paste file{1,2,3,4} | sed -e 's/\t/ \t/g' | column -t -s$'\t'
1   1 2 3       x x x   a a a
2   4 5 6 7 8   yy yy   bb bb bb
                zz      c c cc
                        d d d

Upvotes: 1

lynxlynxlynx
lynxlynxlynx

Reputation: 1433

Just from you examples alone, it seems you could try first joining files 1 and 2, then joining that with file 3, but with a special delimiter, which you'd change later to a space.

Untested example:

paste -d" " file1 file2 | paste -d'|' - file3 | sed 's,|, ,g'

Here I used |, but you should use something you know for sure won't appear in the data i.e. something even more obscure like ˘. A bit of a hack, but should work.

For just two files:

paste -d'¤' file1 file2 | sed 's,¤, ,g'

Upvotes: 2

Related Questions