Pablo
Pablo

Reputation: 11031

Matching files using wildcards

I have a script that tests some APIs that I am designing. This script takes the output of the program, and compares it to the expected output (Master file). If they match, then it returns no errors. I want to be able to mask out things like dates, memory addresses, etc.

Here's an example:

==== MASTER file ====
TEST STARTED ON .*
ADDING ELEMENT 5 to HASHTABLE. Location 0x.*
LOOKING UP ELEMENT 5. SUCCESFUL.
REMOVING ELEMENT
==== END ====

And a file to match it with:

==== OUTPUT file ====
TEST STARTED ON NOV/23 12:18
ADDING ELEMENT 5 to HASHTABLE. Location 0x51F56E2
ROOKING UP ELEMENT 5. SUCCESFUL.
REMOVING ELEMENT
==== END ====

Is there a way to ask diff to use these wildcards? Or is there any other program that does this?

Thanks a lot.

Upvotes: 0

Views: 58

Answers (1)

bardo
bardo

Reputation: 363

You can't do that with a simple diff. You could do something like this though:

set -f
while IFS=, read pattern match ; do
  grep "$pattern" >/dev/null <<<"$match" || { echo ERROR ; exit 1 ; }
done < <(paste -d, master output)

Assuming master is the master file and output is the output file.

There are several important things to keep in mind:

  1. The master file should contain regexps, so all lines should start with a ^.
  2. set -f is necessary to disable shell expansion, otherwise unintended consequences may arise.
  3. Regexps should be properly escaped since they are called through variable substitution, thus needing double quotes.
  4. $IFS must be set to a character neither used in the master file nor in the output file. Set paste's -d paramenter accordingly.

Upvotes: 1

Related Questions