samedi 27 juin 2015

How to get the placeholder's value which is stored in a different file (same directory)

With the conditions:

  1. I cannot use any XML parser tool as I don't have permission , read only

  2. My xmllint version does not support xpath, and I cannot update it , read only

  3. I dont have xmlstarlet and cannot install it

  4. I run my script using Java JSch exec channel

So we have 3 files in a directory.

  1. sample.xml
  2. values1.properties
  3. values2.properties

The contents of the files are as follows:

Sample.xml

<block>
 <name>Bob</name>
 <address>USA</address>
 <email>$BOB_EMAIL</email>
 <phone>1234567</phone>
</block>

<block>
 <name>Peter</name>
 <address>France</address>
 <cell>123123123</cell>
 <drinks>Coke</drinks>
 <car>$PETER_CAR</car>
 <bike>Mountain bike</bike>
</block>

values1.properties

JOE_EMAIL=joe@google.com
BOB_EMAIL=bob@hotshot.com
JACK_EMAIL=jack@jill.com
MARY_EMAIL=mary@rose.com
PETER_EMAIL=qwert1@abc.com

values2.properties

JOE_CAR=Honda
DAISY_CAR=Toyota
PETER_CAR=Mazda
TOM_CAR=Audi
BOB_CAR=Ferrari

I use this script to get the xml block to be converted to a properties file format

NAME="Bob"
sed -n '/name>'${NAME}'/,/<\/block>/s/.*<\(.*\)>\(.*\)<.*/\1=\2/p' sample.xml

OUTPUT:

name=Bob
address=USA
email=$BOB_EMAIL
phone=1234567

How do I get the value of $BOB_EMAIL in values1.properties and values2.properties. Assuming that I do not know where it is located between the two (or probably more) properties file. Bacause it should work differently if I entered

Name=Peter

in the script, it should get

name=Peter
address=France
cell=123123123
drinks=Coke
car=$PETER_CAR
bike=Mountain bike

and the think that will be searched will be PETER_CAR

EXPECTED OUTPUT:

Name=Bob

name=Bob
address=USA
email=bob@hotshot.com
phone=1234567

Name=Peter

name=Peter
address=France
cell=123123123
drinks=Coke
car=Mazda
bike=Mountain bike

Aucun commentaire:

Enregistrer un commentaire