复制unix中两个文件之间的差异

时间:2013-08-05 19:08:35

标签: linux bash unix

首先,哪个是最好和最快的unix命令才能获得两个文件之间的差异?我尝试使用diff来做(下图)。

我尝试了Neilvert Noval在这里给出的答案 - Compare two files line by line and generate the difference in another file

代码 -

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

但是,我得到了很多空格和一个>符号也在不同的行之前。我该如何解决这个问题?我正在考虑删除尾随空格和第一个'>',但不确定这是否是一个整洁的修复。

我的file1.txt有 -

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

我的file1.txt有 -

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

输出 - “#Many space here>很高兴来到这里:)”

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

3 个答案:

答案 0 :(得分:4)

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

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

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

答案 1 :(得分:1)

diff的-y选项使它产生“并排”差异,这就是你有空格的原因。尝试使用零行上下文的统一格式-u 0。应打印出来:

+Glad to be here:)

加号表示添加了行,而减号表示已删除。

答案 2 :(得分:-1)

diff -a --suppress-common-lines -y file1.txt file2.txt|tr 'a >' '' |awk '{print $1}' >>file3.txt 
相关问题