I’m writing a script to find out the diff between files using the GNU version of the diff command. Here I need to ignore the html comment <!– and any patterns (provided as input through a file) that is matched.
File wxy/a:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
some text here
<property name="loginUrl" value="http://localhost:15040/ab/ssoLogin"/>
<!--property name="cUrl" value="http://localhost:15040/ab/ssoLogin" /-->
</beans>
File xyz/a:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
some text there
<property name="loginUrl" value="http://localhost:15045/ab/ssoLogin"/>
<!--property name="cUrl" value="http://localhost:15045/ab/ssoLogin" /-->
</beans>
Pattern input file: input.conf:
[a]
http://.*[:0-9]*/ab/ssoLogin
[some other file]
....
....
My script would read the input.conf for the filename [a] and puts to a temp file lines_to_ignore, now I read the file lines_to_ignore and append the pattern to a variable like below
compare_file.sh
diff_ignore_options="-I \"\!--\"" # Ignore option for <!-- Comments
for iline in `cat lines_to_ignore`; do
diff_ignore_options=${diff_ignore_options}" -I \"$iline\""
echo "-----------------------------------------------------------"
diff -I "\!--" -I "$iline" wxy/a xyz/a
echo "-----------------------------------------------------------"
done
diff $diff_ignore_options wxy/a xyz/a
Now the output:
-----------------------------------------------------------
19c19
< some text here
---
> some text there
-----------------------------------------------------------
19,21c19,21
< some text here
< <property name="loginUrl" value="http://localhost:15040/ab/ssoLogin"/>
< <!--property name="cUrl" value="http://localhost:15040/ab/ssoLogin" /-->
---
> some text there
> <property name="loginUrl" value="http://localhost:15045/ab/ssoLogin"/>
> <!--property name="cUrl" value="http://localhost:15045/ab/ssoLogin" /-->
Why is the variable substitution in diff command not working?
diff $diff_ignore_options wxy/a xyz/a
I want to do it the variable way because I might have to match more than one pattern in some files.
The problem is the
!character, which the shell uses for history expansion. Furthermore, you’re including escaped double-quote characters in your$diff_ignore_optionsvariable; since the pattern you want to ignore doesn’t include any"characters, you don’t want that.This should work (note the use of single quotes to avoid treating
!as a metacharacter):And you can then add more patterns like this: