How to Ignore Some Differences in Diff Command

How to ignore some differences in diff command?

You could filter the two files through sed to eliminate the lines you don't care about. The general pattern is /regex1/,/regex2/ d to delete anything between lines matching two regexes. For example:

diff <(sed '/abXd/,/abYd/d' file1) <(sed '/abXd/,/abYd/d' file2)

diff: how to use '--ignore-matching-lines' option

That not how the -I option in diff works, see this Giles's comment on Unix.SE and also on the man page - 1.4 Suppressing Differences Whose Lines All Match a Regular Expression

In short, the -I option works, if all the differences (insertions/deletions or changes) between the files match the RE defined. In your case, the diff between your two files, as seen in the output

diff f1 f2
4c4
< ddd
---
> #ddd

i.e. 4th line change in both the files, ddd and #ddd are the "hunks" as defined in the man page, together don't match any of your REs #, #.* or ^#.*. So when such an indifference exists, the action will be to print both the matching and the non-matching lines. Quoting the manual,

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

The same would have worked better, if the file f1 did not contain the line ddd, i.e.

f1

aaa
bbb
ccc
eee

f2

aaa
bbb
ccc
#ddd
eee

where doing

diff f1 f2
3a4
> #ddd

would result in just one "hunk", #ddd which can be marked for ignoring with a pattern like ^# i.e. ignore any lines starting with a #, as you can see will produce the desired output (no lines)

diff -u -I '^#' f1 f2 

So given your input contains the uncommented line ddd in f1, it will be not straightforward to define an RE to match a commented and an uncommented line. But diff does support including multiple -I flags as

diff -I '^#' -I 'ddd' f1 f2

but that cannot be valid, as you cannot know the exclude pattern beforehand to include in the ignore pattern.

As a workaround, you can simply ignore lines starting with # on either of the files, before passing it to diff i.e.

diff <(grep -v '^#' f1) <(grep -v '^#' f2)
4d3
< ddd

How ignore specific lines of file using Regex and diff utility (-I regex option)?

I need to check that string does not contain words waiver_id and userdata_hidden.

^(?!.*\bwaiver_id\b)(?!.*\buserdata_hidden\b)

If you don't want any one string to be presented.

^(?!.*\b(?:userdata_hidden|waiver_id)\b)

RUbular

How can I diff 2 files while ignoring leading white space

diff has some options that can be useful to you:

   -E, --ignore-tab-expansion
ignore changes due to tab expansion

-Z, --ignore-trailing-space
ignore white space at line end

-b, --ignore-space-change
ignore changes in the amount of white space

-w, --ignore-all-space
ignore all white space

-B, --ignore-blank-lines
ignore changes whose lines are all blank

So diff -w old new should ignore all spaces and thus report only substantially different lines.

diff while ignoring patterns within a line, but not the entire line

It isn't exactly what you are looking for since I'm not sure how to retain the dates, but this does solve a couple of your issues:

diff -u --label=file1 <(sed 's/^\[....-..-..\]//' file1) --label=file2 <(sed 's/^\[....-..-..\]//' file2)

Output:

--- file1
+++ file2
@@ -1,2 +1,2 @@
- Some random text foo
+ Some random text bar
More output here

can the 'diff' command ignore different kinds of line breaks?

diff --strip-trailing-cr file1 file2

or

diff <(dos2unix < file_from_windows) file_from_osx

or

diff <(tr -d '\r' < file_from_windows) file_from_osx


Related Topics



Leave a reply



Submit