Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two files and applying the differences

on a Linux based system, I can easily compare two files, e.g.:

diff file1.txt file2.txt

...and see the difference between them.

What if I want to take all lines that are unique to file2.txt and apply them to file1.txt so that file1.txt will now contain everything it had + lines from file2.txt that it didn't have before? Is there an easy way to do it?

like image 285
Mordechai Avatar asked May 07 '26 12:05

Mordechai


1 Answers

Using patch

  1. You can use diff's output to create a patch file.

    diff original_file file_with_new_lines > patch_file
    
  2. You can edit patch_file to keep only the additions, since you only want the new lines.

  3. Then you can use the patch command to apply this patch file:

    patch original_file patch_file
    
like image 84
Ronan Boiteau Avatar answered May 10 '26 05:05

Ronan Boiteau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!