简体   繁体   English

如何显示两个文件之间的合并差异?

[英]How to show merged differences between two files?

How can I get only diff letters between two files? 我如何才能在两个文件之间仅获取区分字母?

For example, 例如,

file1: 文件1:

 aaa;bbb;ccc 123;456;789 a1a;b1b;c1c 

file2: 文件2:

 aAa;bbb;ccc 123;406;789 a1a;b1b;c5c 

After diff I should get only this string of difference from the second file: A05 在diff之后,我应该只从第二个文件中得到以下差异字符串: A05

diff -y --suppress-common-lines <(fold -w 1 file1) <(fold -w 1 file2) |
sed 's/.*\(.\)$/\1/' | paste -s -d '' -

This uses process substitution with fold to turn each file into a column of characters that's one character wide and then compares them with diff . 这使用带有fold进程替换将每个文件转换成一字符宽的字符列,然后将它们与diff

The -y option prints lines next to each other, and --suppress-common-lines skips lines that are the same between both files. -y选项打印相邻的行,而--suppress-common-lines跳过两个文件之间相同的行。 Until here, the output looks like this: 直到这里,输出看起来像这样:

$ diff -y --suppress-common-lines <(fold -w 1 file1) <(fold -w 1 file2)
a                                 | A
5                                 | 0
1                                 | 5

We're only interested in the the last character of each line. 我们只对每行的最后一个字符感兴趣。 We use sed to discard the rest: 我们使用sed丢弃其余部分:

$ diff -y --suppress-common-lines <(fold -w 1 file1) <(fold -w 1 file2) |
> sed 's/.*\(.\)$/\1/'
A
0
5

To get these into a single line, we pipe to paste with the -s option (serial) and the empty string as the delimiter ( -d '' ). 为了使它们成一行,我们使用-s选项(序列)和空字符串作为分隔符( -d '' )进行paste The dash tells paste to read from standard in. 破折号告诉paste从标准输入读取。

$ diff -y --suppress-common-lines <(fold -w 1 file1) <(fold -w 1 file2) |
> sed 's/.*\(.\)$/\1/' | paste -s -d '' -
A05

An alternative, if you have the GNU diffutils at your disposal, is cmp : 如果您可以使用GNU diffutils,则可以选择使用cmp

$ cmp -lb file1 file2 | awk '{print $5}' | tr -d '\n'
A05

cmp compares files byte by byte. cmp逐字节比较文件。 The -l option ("verbose") makes it print all the differences, not just the first one; -l选项(“ verbose”)使它打印所有差异,而不仅仅是第一个差异。 the -b options make it add the ASCII interpretation of the differing bytes: -b选项使它添加不同字节的ASCII解释:

$ cmp -lb file1 file2
 2 141 a    101 A
18  65 5     60 0
34  61 1     65 5

The awk command reduces this output to the fifth column, and tr removes the newlines. awk命令将此输出减少到第五列, tr删除换行符。

For the example given, you could compare the files character by character and if there is a difference, print the character of the second file. 对于给出的示例,您可以逐个字符地比较文件,如果有差异,请打印第二个文件的字符。 Here's one way to do that: 这是一种方法:

paste <(fold -w1 file1) <(fold -w1 file2) | \
while read c1 c2; do [[ $c1 = $c2 ]] || printf $c2; done

For the given example, this will print A05 . 对于给定的示例,这将打印A05

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM