Ignore asterix (*) when running diff on Linux

I am trying to use the standard diff command in Linux inorder to look for differences in 2 files. The contents of the file are as follows:

File1

Jim
Jack
Tracy*
Michelle

File2

Jim
Jack
Tracy
Michael

diff File1 File2 gives me the following:

< Tracy*
< Michelle
---
> Tracy
> Michael

However, I want diff to ignore the asterix (*) and produce the following result:

< Michelle
---
> Michael

Can this be done?

+5
source share
3 answers

Using the ShinTakezou approach, but this time using sed:

diff <(sed 's/\*$//' file1) <(sed 's/\*$//' file2)
+1
source

Try

diff -I '*$' FILE1 FILE2

-I RE --ignore-matching-lines = RE

Ignore changes whose rows match RE

Note: this only works with asterisks ending with line ending.

+4
source

If you use diff, which has no option -I, you can cross out lines containing stars into temp files, and then split the temporary files. If you use bash, you can use the "two channels", but if you have a chance that you have diff with the parameter -I. In any case, it will be

sed 's/*$//' file1 >file1.temp
sed 's/*$//' file2 >file2.temp
diff file1.temp file2.temp

or

diff <(sed 's/*$//' file1) <(sed 's/*$//' file2)

(not tested, but it can work in other shells)

the “star” note is deleted, and in terms of differences it never existed.

0
source

All Articles