Help with reading file with values and updating another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with reading file with values and updating another file
# 1  
Old 10-22-2010
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
Code:
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
Code:
<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.

Last edited by Scott; 10-23-2010 at 11:50 PM.. Reason: Code tags, please...
# 2  
Old 10-22-2010
Try something like this,

Code:
$ 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.

Last edited by agn; 10-22-2010 at 05:12 AM..
This User Gave Thanks to agn For This Post:
# 3  
Old 10-22-2010
Quote:
Originally Posted by agn
Try something like this,

Code:
$ 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.
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 ----------




Quote:
Originally Posted by agn
Try something like this,

Code:
$ 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.
cannot use perl any other suggestions please?
# 4  
Old 10-23-2010
any further suggestions?

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
# 5  
Old 10-23-2010
Code:
# ./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>

Code:
### 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

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Reading Xml file and print the values into the text file in columnwise?

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (4 Replies)
Discussion started by: sravanreddy
4 Replies

2. UNIX for Dummies Questions & Answers

Reading XML file and print the values in the text file using Linux shell script

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (1 Reply)
Discussion started by: sravanreddy
1 Replies

3. Shell Programming and Scripting

Reading and Comparing values of file

Hi gurus.. Am reading a file, counting number of lines and storing it in a variable. Then am passing that variable into If loop for comparision, if the number of lines are greater than 1000 it should split a file if not it should send the file name to archive folder.. but when i execute the... (4 Replies)
Discussion started by: azherkn3
4 Replies

4. Shell Programming and Scripting

Reading off values from a large file

Hi, I have a large output file (star.log), with many lines of the following type *** T vavg unburnt: 723.187 / burnt: 2662.000 What I would like to do is pick the values 723.187 and 2662.000 and What I've got so far is awk '/unburnt:.*burnt:/{Tu=$6;Tb=$NF}END{print Tu, Tb}'... (6 Replies)
Discussion started by: lost.identity
6 Replies

5. Shell Programming and Scripting

reading and updating property file

I have to do a read operation for a field in property file, which looks like follows: 123|xyz|datetime|count '|' is delimiter. Finally I managed to read the contents of property file using statement like cut -d"|" -f1 $PROPERTIES_FILE | tr '\n' ' ' <-- Reading first column But now I... (2 Replies)
Discussion started by: rakeshranjanscs
2 Replies

6. Shell Programming and Scripting

Reading values from a file

Hi I have a file in the following format AFUE 0. AOXI 0. VFUE 100.0 VOXI 274.601 TFUE 298. TOXI 2229.544 TMAX 2400. What I want to do is write a bash script, that use either perl/awk or sed to read the number after VFUE and VOXI (which is 100.0 and... (1 Reply)
Discussion started by: lost.identity
1 Replies

7. Shell Programming and Scripting

Reading from one file and updating other file with constant entries

Hi All, Thaks for the help in my last thread. I have one more question. I have two files with ldap entries in it. One file contains all the user LDAP parameter entries (26 MB) and other file contains only the user id's that have to be inactivated. Unix script has to read from the file that has... (8 Replies)
Discussion started by: Samingla
8 Replies

8. Shell Programming and Scripting

Reading values from a file using DB2 SQL

I have some alphbetical codes in that (1 Reply)
Discussion started by: kavithakuttyk
1 Replies

9. UNIX for Dummies Questions & Answers

Reading from a file and assigning values

HI I have something like this in a file ABC = 1 DEF = 2 GHI = 3 JKL = 4 MNO = 5 QRS = 6 TUV = 7 I need to assign ABC to V_abc (that is to a variable) GHI to V_ghi (that is to another variable) TUV to say V_tuv ... (6 Replies)
Discussion started by: ssuresh1999
6 Replies
Login or Register to Ask a Question