diff - 如何忽略空行

Chr*_*ris 10 linux diff

我需要区分两个文件,忽略所有空格和空/空白行,但由于某些原因,我发现 diff 选项做得并不好,它一直显示 file1 中的空行...

$ cat file1
2 nodes configured
13 resources configured

$ cat file2
2 nodes configured
23 resources configured
$ diff -ywBEZb -W 200 --suppress-blank-empty --suppress-common-lines file1 file2
13 resources configured                                                                            |    23 resources configured
                                                                                                   <
$ od -bc file1
0000000 062 040 156 157 144 145 163 040 143 157 156 146 151 147 165 162
          2       n   o   d   e   s       c   o   n   f   i   g   u   r
0000020 145 144 012 061 063 040 162 145 163 157 165 162 143 145 163 040
          e   d  \n   1   3       r   e   s   o   u   r   c   e   s
0000040 143 157 156 146 151 147 165 162 145 144 012 012
          c   o   n   f   i   g   u   r   e   d  \n  \n
0000054
$ od -bc file2
0000000 062 040 156 157 144 145 163 040 143 157 156 146 151 147 165 162
          2       n   o   d   e   s       c   o   n   f   i   g   u   r
0000020 145 144 012 062 063 040 162 145 163 157 165 162 143 145 163 040
          e   d  \n   2   3       r   e   s   o   u   r   c   e   s
0000040 143 157 156 146 151 147 165 162 145 144 012
          c   o   n   f   i   g   u   r   e   d  \n
0000053
$ diff -ywBEZb -W 200 --suppress-blank-empty --suppress-common-lines file1 file2 | od -bc -
0000000 061 063 040 162 145 163 157 165 162 143 145 163 040 143 157 156
          1   3       r   e   s   o   u   r   c   e   s       c   o   n
0000020 146 151 147 165 162 145 144 011 011 011 011 011 011 011 011 011
          f   i   g   u   r   e   d  \t  \t  \t  \t  \t  \t  \t  \t  \t
0000040 011 040 040 040 174 011 062 063 040 162 145 163 157 165 162 143
         \t               |  \t   2   3       r   e   s   o   u   r   c
0000060 145 163 040 143 157 156 146 151 147 165 162 145 144 012 011 011
          e   s       c   o   n   f   i   g   u   r   e   d  \n  \t  \t
0000100 011 011 011 011 011 011 011 011 011 011 040 040 040 074 012
         \t  \t  \t  \t  \t  \t  \t  \t  \t  \t               <  \n
0000117
$
Run Code Online (Sandbox Code Playgroud)

13d*_*tar 16

使用-B开关:

-B  --ignore-blank-lines  Ignore changes whose lines are all blank.
Run Code Online (Sandbox Code Playgroud)

要忽略空格,请使用-b-w开关:

-b  --ignore-space-change  Ignore changes in the amount of white space.
-w  --ignore-all-space  Ignore all white space.
Run Code Online (Sandbox Code Playgroud)

或者干脆RTM

编辑:

由于-B(和其他一些diff开关)似乎不起作用(我没有找到任何信息是否报告为错误),您需要使用不同的方式来忽略空行和空格。

我会建议这样的事情:

[my@pc ~]$ cat file1.txt
2 nodes configured

13 resources configured

[my@pc ~]$ cat file2.txt
2 nodes configured
23 resources configured
[my@pc ~]$ diff <(grep -vE '^\s*$' file1.txt)  <(grep -vE '^\s*$' file2.txt)
2c2
< 13 resources configured
---
> 23 resources configured
Run Code Online (Sandbox Code Playgroud)

  • 我不确定您是否在我的帖子中注意到了它,但是我在 diff 命令中使用了这些选项:$ diff -ywBEZb .... (2认同)