Merge two files line by line with the delimiter triple pipe symbol “|||”

awksedtext processing

I have two parallel files with the same number of lines in two languages and plan to merge these two files line by line with the delimiter |||. E.g., the two files are as follows:

File A:

1Mo 1,1 I love you.
1Mo 1,2 I like you.
Hi 1,3 I am hungry.
Hi 1,4 I am foolish.

File B:

1Mo 1,1 Ich liebe dich.
1Mo 1,2 Ich mag dich.
Hi 1,3 Ich habe Durst.
Hi 1,4 Ich bin neu.

The expected output is like this:

1Mo 1,1 I love you. ||| 1Mo 1,1 Ich liebe dich.
1Mo 1,2 I like you. ||| 1Mo 1,2 Ich mag dich.
Hi 1,3 I am hungry. ||| Hi 1,3 Ich habe Durst.
Hi 1,4 I am foolish. ||| Hi 1,4 Ich bin neu.

I tried the paste command such as:

paste -d "|||" fileA fileB

But the returned output is only containing one pipe such as:

1Mo 1,1 I love you. |1Mo 1,1 Ich liebe dich.
1Mo 1,2 I like you. |1Mo 1,2 Ich mag dich.

Is there any way to separate each pair of lines by tripe pipe |||?

Best Answer

With POSIX paste:

:|paste -d ' ||| ' fileA - - - - fileB

paste will concatenate corresponding lines of all input files. Here we have six files, fileA, four dummy files from standard in -, and fileB.

The list of delimiters include a space, three pipe and a space in that order will be used by paste circularly.

For the first line of six files, fileA will be concatenated with the first dummy file (which is nothing, thank to the no-op : operator), produce line1-fileA<space>.

The first dummy file will be concatenated with the second by a pipe, produce line1-fileA |, then the second dummy file with the third dummy file, produce line1-fileA ||, the third dummy file with the the forth dummy file, produce line1-fileA |||.

And the forth dummy file with fileB, produce line1-fileA ||| line1-fileB.

Those step will be repeated for all lines, give you the expected result.


The use of :| is for less-typing, and mainly use in interactive shell. In a script, you should use:

</dev/null paste -d ' ||| ' fileA - - - - fileB

to prevent a subshell from being spawned.

Related Question