skoestlmeier
skoestlmeier

Reputation: 220

Merge multiple text files with different length

I have multiple files with simple format like

File1   File2   File3
|1|     |2|     |4|
        |3|     |5|
                |6|

All files have different length. I try to get the following output:

|1,2,4|
|,3,5 |
|,,6  |

So each row of the output contains each row of the equivalent input files with a comma as delimiter.

Running the following command

paste -d',' input1 input2

for two input files gives the suitable output above. However, running the command for three input files results in

|1,2|
|,4 |
|,3 |
|,5 |
...

Why does the command for two files fail with three or more files?

Upvotes: 3

Views: 285

Answers (1)

bishop
bishop

Reputation: 39364

Quick and dirty:

$ cat file1
|1|
$ cat file2
|2|
|3|
$ cat file3
|4|
|5|
|6|
$ paste -d ',' file[123] | sed -e 's/|//g' -e 's/^/|/g' -e 's/$/|/g'
|1,2,4|
|,3,5|
|,,6|

Upvotes: 1

Related Questions