如何使用diff查看空白更改? [英] How can I use diff to see whitespace changes?

查看:47
本文介绍了如何使用diff查看空白更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了这个问题,其中包含的答案git diff .但是,我并没有使用任何类型的版本控制来比较文件(我什至在试图与之进行比较的计算机上都没有可用的文件控制.)

I found this question which has answers for git diff. However, I am not comparing files using any sort of version control (I don't even have one available on the machine I am trying to compare from).

基本上,与引用的问题类似,我试图查看空白中的更改. diff 命令可能显示:

Basically, similar to the referenced question, I am trying to see the changes in whitespace. The diff command might show:

bash-3.2$ diff 6241 6242
690c690   
<         
---       
>         

但是我不知道这是换行符,换行符和空格还是什么.我需要知道两个文档之间的确切变化,包括空格.我已经尝试过 cmp -l -b ,它可以工作,但是当发生很多变化而实际上也没有用时,很难读懂.

But I don't know if that is a newline, a newline and space, or what. I need to know the exact changes between two documents, including whitespace. I have tried cmp -l -b and it works, but it is rather difficult to read when there are a lot of changes to the point where it isn't really useful either.

我真正想要的是某种以某种方式呈现空白的方式,这样我就可以准确地知道空白是什么,例如颜色或^ J,^ M等.我在手册中看不到任何内容; diff --version 显示GNU版本2.8.1.

What I really want is some way for whitespace to be rendered in some way so I can tell exactly what the whitespace is, e.g. color or perhaps ^J, ^M, etc. I don't see anything in the manual; diff --version shows GNU version 2.8.1.

作为另一个示例,我还尝试通过 hexdump 传递 diff 的输出.

As a further example, I have also tried piping the output of diff through hexdump.

bash-3.2$ diff 6241 6242 | hexdump -C                                         
00000000  36 39 30 63 36 39 30 0a  3c 20 0a 2d 2d 2d 0a 3e  |690c690.< .---.>|
00000010  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
00000020  20 20 20 20 0a                                    |    .|           

据此,对我来说很明显添加了一堆空格字符.但是,不明显的是在换行符之前插入了一个空格,这就是 cmp 告诉我的:

From this it is obvious to me that a bunch of space characters were added. However, what is not obvious is that a space was inserted before the newline, which is what cmp tells me:

bash-3.2$ cmp -l -b 6241 6242
33571  12 ^J    40           
33590  40       12 ^J        
33591 165 u     40           
...

推荐答案

单独使用 diff 命令没有简单的方法来做到这一点.解决问题的一种方法是使用 cat -te ,它将制表符转换为 ^ I ,并在行尾写入 $ ,这样更易​​于查看.

There is no easy way to do this with the diff commmand alone. One way to solve your problem is to use cat -te which will turn tab characters into ^I and will write $ at the end of lines, making it easier to see.

$ printf >test1 'hello \t \n'
$ printf >test2 'hello  \t\n'
$ diff test[12] | cat -te
1c1$
< hello ^I $
---$
> hello  ^I$

这篇关于如何使用diff查看空白更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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