2

I have two Files test1:

[BEFORE_TEST] WARN: message1
[BEFORE_TEST] NOTICE: message 

test2:

[AFTER_TEST]  PASS: message1
[AFTER_TEST]  NOTICE: message

test2 is the fixes that i made based on test1, now i want to use diff to show the differences ignoring [BEFORE_TEST] and [AFTER_TEST] in both files. Problem is NOTICE is being considered for "difference" due to change in tags from [BEFORE_TEST] to [AFTER_TEST]. Is there any way we can ignore these specific words?

2
  • 2
    diff test1 test2 | grep -v [BEFORE_TEST]||[AFTER_TEST]? Commented Nov 14, 2017 at 15:53
  • Ignoring the tags may be insufficient. There is one space character before NOTICE, then a trailing space after message in test1, but respectively two space characters and no trailing space in test2. Is this just clumsiness of the question? Or is this a real thing and you want solutions that ignore such differences as well? Commented Jan 4, 2024 at 16:24

2 Answers 2

1

The common approach is to remove (as shown by @RomanPrrekhrest) or replace those things with something that is the same for both files.

For instance, here you could replace both [BEFORE_TEST] and [AFTER_TEST] with [*_TEST], or you could replace some Sep 8 18:10:03 timestamps with <timestamp> or Mmm dd HH:MM:SS or *** ** **:**:**.

massage() {
  sed -Ee '
    s/\[[[:alnum:]_]+_TEST\]/[*_TEST]/g
    s/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [ 0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]/*** ** **:**:**/g
    ' -- "$@"
}
diff -w -u -- <(massage test1) <(massage test2)

(here also using -w as there are also differences in whitespace between your two files)

Which gives:

$ diff -wu <(massage test1) <(massage test2)
--- /proc/self/fd/11    2024-09-12 06:13:08.008529281 +0100
+++ /proc/self/fd/18    2024-09-12 06:13:08.008529281 +0100
@@ -1,2 +1,2 @@
-[*_TEST] WARN: message1
+[*_TEST]  PASS: message1
 [*_TEST] NOTICE: message

With some sed implementations, you can replace the [[:alnum:]_] with \w and in some (rarer) [0-9] with \d (or switch to perl -pe which recognises both).

As a cherry on the cake, you could also do (assuming GNU stat and GNU diff):

massaged_label() {
  stat -c $'--label=massaged_%n\t%y' -- "$@"
}
diff -w -u "$(massaged_label test1)" "$(massaged_label test2)" \
         -- <(massage        test1)   <(massage        test2)

Which gives:

--- massaged_test1      2024-09-12 06:12:55.376015083 +0100
+++ massaged_test2      2024-09-12 06:13:03.495702763 +0100
@@ -1,2 +1,2 @@
-[*_TEST] WARN: message1
+[*_TEST]  PASS: message1
 [*_TEST] NOTICE: message

Where the header indicates which files are being diffed along with their last modification time and that they've been massaged.

In any case, that output cannot be used as input to patch (or only on the massaged versions of the files).

0

I suppose, you need to eliminate those parts through removing them while searching differences:

wdiff <(sed 's/^\[[^]]\+\] *//' test1) <(sed 's/^\[[^]]\+\] *//' test2)

The output:

[-WARN:-]{+PASS:+} message

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.