Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two java property files using shell script [closed]

Tags:

java

shell

unix

How to compare two property files in two different folders using shell script input can be path of the two folders?

There are multiple files in both folders and each files needs to be checked

like image 625
vetri02 Avatar asked Oct 28 '09 10:10

vetri02


4 Answers

You probably need to explain your requirements a bit more. However it's most likely you can do what you want with the diff command (with a little help from sort and/or grep).

Let's assume you have two files: a.properties and b.properties

If you simply want to know if the files are different in any way, you can use

diff a.properties b.properties

You'll get no output if they're identical or a list of differences.

If you want a comparison on a more semantic level, that is, are the two sets of properties identical, then you need to do a bit more. The files can differ textually, but mean the same thing to Java programs that use them. For example, the properties can occur in a different order. There can be blank lines, other whitespace and comments.

If this is the case, do you care if the comments are identical? They won't effect the operation of your program, but they have a meaning (and value to those reading the file). If you don't care, strip them out.

You probably don't care about blank lines as they have no meaning.

You also need to handle the following case:

a.properties:
    prop = value
b.properties:
    prop=value

Again, different textually (note the spaces around the equals) but have the same meaning in Java.

Starting simple, let's assume the properties occur in the same order.

Ignore blank lines:

diff -B a.properties b.properties

Handle random white space (eg. around the equals sign)

diff -w a.properties b.properties

Combine all of this:

diff -w -B a.properties b.properties

Strip out comments:

grep -v '^#.*$' a.properties > a.tmp
grep -v '^#.*$' b.properties > b.tmp
diff -w -B a.tmp b.tmp
rm a.tmp b.tmp

Allow for properties in a different order, strip comments:

grep -v '^#.*$' a.properties | sort > a.tmp
grep -v '^#.*$' b.properties | sort > b.tmp
diff -w -B a.tmp b.tmp
rm a.tmp b.tmp
like image 187
dave Avatar answered Nov 10 '22 01:11

dave


You should look into using diff or sdiff. I would recommend sorting your files beforehand and removing any blank lines to reduce the amount of noise; e.g.

file1=/var/tmp/foo.txt
file2=/var/tmp/bar.txt

sort ${file1} | grep -v '^$' > ${file1}.tmp
sort ${file2} | grep -v '^$' > ${file2}.tmp
sdiff ${file1} ${file2}
like image 33
Adamski Avatar answered Nov 09 '22 23:11

Adamski


For semantic comparison, better use PropDiff.

Usage: [flags] properties-file1 properties-file2 [-f filenameOrPathPrefixForResults]
   flags:
     -c  property settings that are common to both p1 and p2, where p2 take precedence
     -u  union p1 and p2 where p2 has higher precedence
     -1  properties settings that are only in p1
     -2  properties settings that are only in p2
     -d  intersection of properties in p1 and p2 that have different values
     -e  intersection of properties in p1 and p2 that have equal values
like image 37
fglez Avatar answered Nov 10 '22 00:11

fglez


comm is a useful utility. It can tell you what's in file1 and not in file2, what's in file2 and not in file1, and what's common between the two files. It requires the two files to be sorted first.

I upvoted @antispams PropDiff answer since it seems to be exactly what you're asking for.

like image 38
glenn jackman Avatar answered Nov 10 '22 01:11

glenn jackman