3

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?

skoestlmeier
  • 200
  • 8

1 Answers1

1

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|
bishop
  • 37,830
  • 11
  • 104
  • 139
  • I have tried this and the problem still remains with my files. Could this occure due to the new line at the end of each line. Any suggestions? – skoestlmeier Nov 28 '17 at 17:19
  • Post the output of `cat -vet` on each of your files. – bishop Nov 28 '17 at 17:20
  • File Nr. 1 gives the output `13$` and all following files have the form `[0-9]^M$` for each line. This differences seem to be weird, as all input files are generated by the same process. How to interpret these outputs? – skoestlmeier Nov 28 '17 at 18:41
  • The files containing `^M` have DOS-style line endings. You can convert them using `sed`, `tr`, `dos2unix`, and others. See [this question](https://stackoverflow.com/q/82726/2908724). Fix this issue, then the command above should work as posted. – bishop Nov 30 '17 at 14:30
  • 1
    Thanks a lot for your help! I removed the `^M`with the `tr`command and your suggested answer worked fine! – skoestlmeier Nov 30 '17 at 16:17