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
will concatenate corresponding lines of all input files. Here we have six files,fileA
, four dummy files from standard in-
, andfileB
.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), produceline1-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, produceline1-fileA ||
, the third dummy file with the the forth dummy file, produceline1-fileA |||
.And the forth dummy file with
fileB
, produceline1-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:to prevent a subshell from being spawned.