在UNIX两个文件之间复制差异 [英] Copy differences between two files in unix

查看:96
本文介绍了在UNIX两个文件之间复制差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,这是最好的,最快的UNIX命令只得到两个文件之间的差异?我试着使用diff来做到这一点(见下文)。

Firstly, which is the best and fastest unix command to get only the differences between two files ? I tried using diff to do it (below).

我试图Neilvert诺瓦尔在这里给出了答案 - <一个href=\"http://stackoverflow.com/questions/4544709/compare-two-files-line-by-line-and-generate-the-difference-in-another-file\">Compare通过线两个文件行并产生另一个文件的区别

I tried the answer given by Neilvert Noval over here - Compare two files line by line and generate the difference in another file

code -

diff -a --suppress-common-lines -y file1.txt file2.txt >> file3.txt

不过,我得到了很多的空间和>符号也不同行之前。我该如何解决呢?我想删除尾随空格和第一'>',但不知道这是一个整洁的修复。

But, I get a lot of spaces and a > symbol also before the different lines. How do I fix that ? I was thinking of removing trailing spaces and the first '>', but not sure if that is a neat fix.

我FILE1.TXT有 -

My file1.txt has -

Hello World!
Its such a nice day!
#this is a newline and not a line of text# 

我FILE1.TXT有 -

My file1.txt has -

Hello World!
Its such a nice day!
Glad to be here!
#this is a newline and not a line of text# 

输出 - 在这里##Many空格>高兴到这里来:)

Output - " #Many spaces here# > Glad to be here:)"

预期的输出 - 高兴到这里来:)

推荐答案

另一种方式来获得差异是使用awk的:

Another way to get diff is by using awk:

awk 'FNR==NR{a[$0];next}!($0 in a)' file1 file2

虽然我必须承认,我没有运行任何基准,并不能说这是最快的解决方案。

Though I must admit that I haven't run any benchmarks and can't say which is the fastest solution.

这篇关于在UNIX两个文件之间复制差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆