Diff ignoring patterns within the string, but not the entire string

I often have to compare two files, ignoring some changes in these files. I do not want to ignore whole lines, but part of them. The most common case of this is time stamping on lines, but there are dozens of other patterns that I also need to ignore.

File1:

[2012-01-02] Some random text foo
[2012-01-02] More output here

File2:

[1999-01-01] Some random text bar
[1999-01-01] More output here

In this example, I want to see the difference in line number 1, but not in line number 2.

Using the diff -I option will not work because it ignores the entire line. Ideal yield:

--- file1       2013-04-05 13:39:46.000000000 -0500
+++ file2       2013-04-05 13:39:56.000000000 -0500
@@ -1,2 +1,2 @@
-[2012-01-02] Some random text foo
+[1999-01-01] Some random text bar
 [2012-01-02] More output here

I can pre-process these files with sed:

sed -e's/^\[....-..-..\]//' < file1 > file1.tmp
sed -e's/^\[....-..-..\]//' < file2 > file2.tmp
diff -u file1.tmp file2.tmp

but then I need to put these temporary files somewhere and don't forget to clean them later. In addition, my diff output no longer applies to the original file names and no longer emits the original lines.

diff , ?

+5
2

, , , , :

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

:

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

, , :

$ diff <(command with output) <(other command with output)

:

diff <(cat f1 | sed -e's/^\[....-..-..\]//') <(cat f2 | sed -e's/^\[....-..-..\]//')

, .

+1

All Articles