ignore pattern with backward slash in diff command

428 views Asked by At

File foo.c:

do {                                                            
    a=b;
    c=d;
 } while (0)

File bar.c

do {                                                            
    a=b;
    c=d;
\
} while (0)

In shell script the diff of above 2 files:

diff foo.c bar.c

<   } while (0)
---
>     \
>   } while (0)

I am trying to ignore this diff using

diff -I "reg-ex pattern".

I tried pattern like "\s}\swhile\s(0)" , "} while (0)" etc. But no use. Is it possible to ignore the above case? If not "diff", any other utility?

2

There are 2 answers

0
Mohammad Yusuf On

You can do that by deleting the unwanted lines with sed. And then ignoring the differences.

You have 2 differences between foo.c and bar.c

  1. Line with \
  2. There is a space before } in foo.c

You can do something like this:

diff -I '\s\?}' <(sed '/^\\/d' bar.c) foo.c

Delete the line with \ using sed and ignore the space in foo.c

0
Ruslan Osmanov On

Try this:

pat='^[[:space:]]*\\[[:space:]]*$'
sub_pat='s/'"$pat"'//'
diff -B -I "$pat" <(sed -e "$sub_pat" foo.c) <(sed -e "$sub_pat" bar.c)

The first line stores a regular expression into pat variable for the next diff and sed commands. The regular expression matches a line with a backslash (\\) optionally surrounded with space characters.

The sed commands replace the lines matching $pat pattern with empty strings (-B option causes diff to ignore blank lines).


It is impossible to filter out the backslashes from the output of diff without extra tools, unless the only difference is in the lines containing the pattern being ignored:

for each nonignorable change, `diff' prints the complete set of changes in its vicinity, including the ignorable ones.

See answers to this question for details.