diff - 空行を無視する方法

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
$

ベストアンサー1

スイッチを使用してください-B

-B  --ignore-blank-lines  Ignore changes whose lines are all blank.

スペースを無視するには、-bスイッチ-wを使用します。

-b  --ignore-space-change  Ignore changes in the amount of white space.
-w  --ignore-all-space  Ignore all white space.

または単に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

おすすめ記事