3

let's say I have these two commands:

$ cat file1
file1_a
file1_b
file1_c
file1_d

And:

$ cat file2
file2_a
file2_b
file2_c
file2_d

How can I combine these outputs using a custom separator (e.g. ...) so that I get the following output:

$ # some fancy command like { cat file1 & cat file2 } | combine --separator='...'
file1_a...file2_a
file1_b...file2_b
file1_c...file2_c
file1_d...file2_d

?

1
  • It is not a duplicate. The custom separator is more than one character.
    – hschou
    Jul 26, 2016 at 15:59

3 Answers 3

4

I like to use the paste command.

paste -d. file1 - - file2 < /dev/null

produces desired output

file1_a...file2_a
file1_b...file2_b
file1_c...file2_c
file1_d...file2_d  

- refers to stdin, we use this twice to triple our dots </dev/null is used because we do not want anything between those dots.

2
  • Cool, but can I just have two arbitrary commands combined (not just files, but stdout piped output) and pass their output to paste somehow? Jul 25, 2016 at 22:12
  • 3
    paste -d. <(command1) - - <(command2) < /dev/null Jul 25, 2016 at 22:18
1

If you paste two files they will be delimited by a tabulator. So just replace the tabulator with your desired separator:

$ paste file1 file2 | sed -e 's/\t/.../'

If your content has a tabulator then invent a new separator which does not occur like %:

$ paste -d% file1 file2 | sed -e s/%/.../
0

This is @zacharybrady's answer, but to be more like what you asked for. It terms of input processes. That I guess you will extend.

paste -d. <(cat file1) - - <(cat file2) < /dev/null

Thanks to @zacharybrady for the bit I was stuck on. I had got to this join <(cat -n file1) <(cat -n file2) | cut -d" " -f2-, as I had forgotten about paste. I could not get the field separator correct though.

Note: I have discovered that paste is not utf-8 compliant. It only processes the first byte after the -d, so -d… is broken. (I am using debian jessie, paste (GNU coreutils) 8.23)

Not the answer you're looking for? Browse other questions tagged .