jayka
August 23, 2011, 2:04pm
1
Hi All,
I have 2 properties files - one is a master templete and other one is a node specific properties file, I need to comapre these 2 properties files and make sure the node Specific properties file contains all the properties in the master temple properties file else report the missing property
and secondly
if value provided in the master template then same value must be in the node specific file if not report mismatch
here are the 2 example property files :
master-templete.properties
## Proxy name
jboss.proxy.name=
## PTMS Database
db.host.1=
db.port.1=1521
db.host.2=
db.port.2=1521
#EOF
2.nodeOne.properties
## Proxy name
## PTMS Database
db.host.1=10.3.61.55
db.port.1=1521
db.host.2=10.3.61.55
db.port.2=1521
#EOF
awk -v MASTER="/path/to/master.file" -v FS="=" 'BEGIN {
while(getline <MASTER)
{
if ( ($0 ~ /^[ \t]*#/) || ($0 == "") ) continue;
if($2 != "")
SET[$1]=$2
}
}
{
if (!($0 ~ /^[ \t]*#/) && !($0 == "") )
if(SET[$1] && (SET[$1] != $2))
print "ERROR " $0;
}' < /path/to/local.file
---------- Post updated at 12:51 PM ---------- Previous update was at 12:34 PM ----------
Improved version:
awk -v FS="=" -v MASTER="/path/to/master" 'BEGIN { while(getline <MASTER)
{
if (($0 ~ /^[ \t]*#/) || ($0 == "")) continue;
INMASTER[$1]=1; VAL[$1]=$2;
}
}
{ if (!($0 ~ /^[ \t]*#/) && !($0 == "") ) LOCAL[$1]=$2; }
END { for(k in INMASTER)
{
if((VAL[k] == "") && (LOCAL[k] == "")) print "UNSET " k;
if((VAL[k]!="") && (LOCAL[k] != VAL[k])) print "OVERRIDDE " k;
}
}' < /path/to/local
With your input data, and db.port.2=1521 modified to 1522, I get:
UNSET jboss.proxy.name
OVERRIDDE db.port.2
1 Like
jayka
August 24, 2011, 9:24am
3
When I try to run the script below :
I keep getting the following error , I am not sure what I am doing wrong ..
Please help ...
awk: syntax error near line 1
awk: bailing out near line 1
Note : My properties file "master-templete.properties " and "nodeOne.properties" are in teh same directory as the script.
awk -v FS="=" -v MASTER="master-templete.properties" 'BEGIN { while(getline <"MA
STER")
{
if (($0 ~ /^[ \t]#/) || ($0 == "")) continue;
INMASTER[$1]=1; VAL[$1]=$2;
}
}
{ if (!($0 ~ /^[ \t] #/) && !($0 == "") ) LOCAL[$1]=$2; }
END { for(k in INMASTER)
{
if((VAL[k] == "") && (LOCAL[k] == "")) print "UNSET " k;
if((VAL[k]!="") && (LOCAL[k] != VAL[k])) print "OVERRIDDE " k;
}
}' < nodeOne.properties
Use gawk/nawk.
That "MASTER" should be just MASTER -- you aren't trying to read from a file named "MASTER" after all, but a variable named MASTER with a filename in it.
jayka
August 24, 2011, 9:51am
5
Thank you .
Now nawk works , it doesn't do anything just hangs now.
Hmm. This should print info as it goes, hopefully telling you where it freezes:
nawk -v FS="=" -v MASTER="/path/to/master" 'BEGIN {
print "running BEGIN";
while(getline <MASTER)
{
if (($0 ~ /^[ \t]*#/) || ($0 == "")) continue;
INMASTER[$1]=1; VAL[$1]=$2;
print $0;
}
print "BEGIN finished"
}
{ if (!($0 ~ /^[ \t]*#/) && !($0 == "") ) LOCAL[$1]=$2; } 1
END { for(k in INMASTER)
{
if((VAL[k] == "") && (LOCAL[k] == "")) print "UNSET " k;
if((VAL[k]!="") && (LOCAL[k] != VAL[k])) print "OVERRIDDE " k;
}
}' < /path/to/local