[英]Comparing two files and applying the differences
on a Linux based system, I can easily compare two files, eg: 在基于Linux的系统上,我可以轻松地比较两个文件,例如:
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? 如果我想获取
file2.txt
独有的所有行并将它们应用到file1.txt,那么file1.txt
现在将包含它所拥有的所有内容+ file2.txt
中之前没有的行? Is there an easy way to do it? 有一个简单的方法吗?
patch
patch
You can use diff
's output to create a patch file . 您可以使用
diff
的输出来创建补丁文件 。
diff original_file file_with_new_lines > patch_file
You can edit patch_file
to keep only the additions, since you only want the new lines. 您可以编辑
patch_file
以仅保留添加内容,因为您只需要新行。
Then you can use the patch
command to apply this patch file: 然后,您可以使用
patch
命令应用此修补程序文件:
patch original_file patch_file
If you don't mind appending the sorted diff to your file, you can use comm : 如果您不介意将已排序的差异附加到您的文件,您可以使用comm :
cat file1.txt <(comm -13 <(sort f1.txt) <(sort f2.txt)) > file1.txt.patched
or 要么
comm -13 <(sort f1.txt) <(sort f2.txt) | cat file1.txt - > file1.txt.patched
This will append the unique lines from file2.txt to file1.txt. 这会将file2.txt中的唯一行追加到file1.txt。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.