Help with reading file with values and updating another file

Hi
I need some help with a task, i am an absolute newbie to any form of shell scripting and request guidance.

Task:

  1. Read a config file in form of name value pair
    ex
host=www.test.com
port=8080
binding="dynamic" or "other" or "something else"
key1=value1
key2=value2
key3=value4
policy="<wsp  policyFile='mypolicy.policy' >
......

read another file and replace values in the file with value picked up in the first file

second file format

<file  port="7001" binding="static" />
<details   hos="http//test.com" />  
<policy node> insert value of policy here  only if binding=dynamic otherwise leave it
</policy node>

</file>

The key/value's might change and hence is there any way to write a generic script to take care of the key values or even otherwise write a hard wired script to pick up the key's and values and update the file according to the requirement.

any help is appreciated.

Try something like this,

$ cat /tmp/in
host=www.test.com
port=8080
binding="dynamic" or "other" or "something else"
key1=value1
key2=value2
key3=value4
policy="<wsp policyFile='mypolicy.policy' >
$ cat /tmp/in2
<file port="7001" binding="static" />
<details host="http//test.com" />
<policy node> insert value of policy here only if binding=dynamic otherwise leave it
</policy node>

</file>
$ cat test.pl
#!/usr/local/bin/perl -w

open CONFIG, '<', '/tmp/in';
open F, '<', '/tmp/in2';

while (<CONFIG>) {
        ($key, $value) = split /=/,$_,2;
        chomp($config{$key} = $value);
}

while ($line = <F>)  {
        foreach (keys %config) {
                $line =~ s/($_=)[^ ]*/$1"$config{$_}"/;
        }
        print $line;
}
close F;
close CONFIG;
$ perl test.pl
<file port="8080" binding=""dynamic" or "other" or "something else"" />
<details host="www.test.com" />
<policy node> insert value of policy here only if binding=""dynamic" or "other" or "something else"" otherwise leave it
</policy node>

</file>

Modify it to suit your needs.

1 Like

Thank you for the code. It looks like you are using perl to do some processing and I checked with the box admin and he says we do not have perl installed and it is not on the list to be supported. Is there any other way using just shell scripting to achieve this task.

---------- Post updated at 09:59 AM ---------- Previous update was at 09:09 AM ----------

any suggestions?

---------- Post updated at 10:43 AM ---------- Previous update was at 09:59 AM ----------

i cannot use perl any other suggestions please?

thanks

---------- Post updated at 11:38 AM ---------- Previous update was at 10:43 AM ----------

cannot use perl any other suggestions please?

Hi friends,
the solution given above is not workable for me, is there any alternate suggestions that can be of use to me?

thanks in advance
mk

# ./justdoit
<file port="8080" binding=""dynamic" or "other" or "something else"" />
<details host="www.test.com" />
<policy node> insert value of policy here only if binding=""dynamic" or "other" or "something else"" otherwise leave it
</policy node>
 
</file>
### justdoit ###
 
#!/bin/bash
/bin/cp -f file2 orgfile2
for i in `sed '/policy/d;s/\(.*\)=.*/\1/' file1 `
 do
  x=$(sed -n "/$i/s/\(.*=\)\(.*\)/\1\"\2\"/p" file1)
  sed -i "s@$i=[\"0-9a-z\.\/]*@$x@" file2
 done
 more file2