Shell – Compare lines and upgrade two different files

mergeshell-scripttext processing

I have a couple of text files with the following features:

$ cat file_1
Line A
Line B
Line C
Line D

$ cat file_2
Line A
Line added 1
Line B
Line D
Line added 2

They are such that file_1 has some lines that file_2 does not contain and vice-versa. I would like to upgrade each other with the missing lines so that both will become

Line A
Line added 1
Line B
Line C
Line D
Line added 2

The order of file_1 is preserved, but with the integrations coming from file_2 put in the same places as in file_2 (not in the head or in the tail or in random positions).

1) Is it possible to merge this way the files through an appropriate bash script?

2) Is it possible to do the same, when instead of lines I have paragraphs, that is: blocks of lines?

Best Answer

diff file_1 file_2 | grep -Ev '^<|[0-9]+d[0-9]+' | patch file_1
Related Question