10

I need to diff two files ignoring all whitespaces and empty/whitespace lines but for some reasons diff options I found does not do all well, it keeps showing the empty line in 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
  • I think the issue here is that -B doesn't simply ignore blank lines - it ignores changes whose lines are all blank. In your case, the trailing blank line is considered to be part of a contiguous multi-line change starting at 13 resources configured Feb 10, 2018 at 15:03

2 Answers 2

18

Use the -B switch:

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

To ignore whitespaces, use the -b and -w switches:

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

Or simply RTM.

EDIT:

As -B (and some other diff switches) seems to be not working (I didn't find any information whether it's reported as bug), you need to use a different way to ignore blank lines and white spaces.

I would suggest something like this:

[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
2
  • 2
    I am not sure you have noticed it in my post, but I used those options in my diff command: $ diff -ywBEZb ....
    – Chris
    Feb 10, 2018 at 10:30
  • @Chris I've revised my answer.
    – 13dimitar
    Feb 12, 2018 at 9:12
2

I can help with command to delete the empty lines and blank spaces with sed command

To Delete empty or blank lines use below command

sed '/^$/d' filename

To Remove white spaces in lines

sed -r "s/\s+//g" filename

After executing above command you can use diff command to know the difference between 2 files

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .