How to change values in datafile from source file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to change values in datafile from source file
# 1  
Old 08-26-2010
How to change values in datafile from source file

Hi all,

I have this source file source.txt :
Code:
hdrEthernetSwitch1IPAddress 112.13.9.34
hdrEthernetSwitch2IPAddress 112.13.8.245
noOfRevASLAvailable 8000
noOfRevASLInUse 122
maxDOSPayload 7777
pcmdEvdoRUM Enabled
pcmdEvdoFlow Enabled

And I have this datafile to be modified :
Code:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE cmdf SYSTEM "cmdf.dtd">
<cmdf release="20" releaseECP="35.0" snId="5" date="201004190430CDT" >
<frame release="20" releaseECP="35.0" n="FMSFrameId" v="2" >
<entitytype n="FMSFrame" >
<entity >
<keyfld n="hdrsnId" v="5" />
<keyfld n="FMSFrameId" v="2" />
<fld n="hdrRouterIPAddress1" v="0.0.0.0" />
<fld n="hdrRouterIPAddress2" v="0.0.0.0" />
<fld n="hdrEthernetSwitch1IPAddress" v="0.0.0.0" />
<fld n="hdrEthernetSwitch2IPAddress" v="0.0.0.0" />
<fld n="subnetMask" v="104" />
<fld n="colorCode" v="2" />
<fld n="hdrcFMSVCVMIPAddress" v="172.27.11.232" />
<fld n="systemIdentifier" v="1" />
<fld n="ta13ReqTimer" v="2.0" />
<fld n="frameType" v="R1SRFrame" />
<fld n="asrtRptCtrl" v="Enabled" />
<fld n="asrtThrotCtrl" v="Enabled" />
<fld n="cpHighOccRptCtrl" v="Disabled" />
<fld n="cpfailAsrtLogCtrl" v="Enabled" />
<fld n="cpExcepRptCtrl" v="Disabled" />
<fld n="pcmdEvdoEnable" v="Disabled" />
<fld n="pcmdEvdoNormConn" v="Disabled" />
<fld n="pcmdEvdoNormSess" v="Disabled" />
<fld n="pcmdEvdoRUM" v="Disabled" />
<fld n="pcmdEvdoFlow" v="Disabled" />
<fld n="revASLKey" v="1025693969d79fdef19d10c7bb1e794f4f42" />
<fld n="revAActFlag" v="Activate" />
<fld n="noOfRevASLAvailable" v="33" />
<fld n="noOfRevASLInUse" v="4" />
<fld n="maxDOSPayload" v="306" />
<fld n="enable65KUATISessionsPerOHM" v="Yes" />
<fld n="enable128KUATISessionsPerOHM" v="No" />
<fld n="auxRNCCtrl1" v="0" />
<fld n="orcaOMCRANenabled" v="Yes" />
<fld n="auxRNCCtrl2" v="0" />
<fld n="auxRNCCtrl3" v="0" />
<fld n="auxRNCCtrl4" v="0" />
<fld n="intraRNCUATIBalanceEnable" v="No" />
<fld n="intraRNCUATISessionReptEnable" v="Yes" />
<fld n="intraRNCUATIBalanceThreshold" v="70" />
<fld n="uatiOverloadThreshold" v="95" />

How do I get my values in source file to be updated in my datafile? SO I can get updated values in the datafiles as below :
Code:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE cmdf SYSTEM "cmdf.dtd">
<cmdf release="20" releaseECP="35.0" snId="5" date="201004190430CDT" >
 <frame release="20" releaseECP="35.0" n="FMSFrameId" v="2" >
  <entitytype n="FMSFrame" >
   <entity >
    <keyfld n="hdrsnId" v="5" />
    <keyfld n="FMSFrameId" v="2" />
    <fld n="hdrRouterIPAddress1" v="0.0.0.0" />
    <fld n="hdrRouterIPAddress2" v="0.0.0.0" />
    <fld n="hdrEthernetSwitch1IPAddress" v="112.13.9.34" />
   <fld n="hdrEthernetSwitch2IPAddress" v="112.13.8.245" />
    <fld n="subnetMask" v="104" />
    <fld n="colorCode" v="2" />
    <fld n="hdrcFMSVCVMIPAddress" v="172.27.11.232" />
    <fld n="systemIdentifier" v="1" />
    <fld n="ta13ReqTimer" v="2.0" />
    <fld n="frameType" v="R1SRFrame" />
    <fld n="asrtRptCtrl" v="Enabled" />
    <fld n="asrtThrotCtrl" v="Enabled" />
    <fld n="cpHighOccRptCtrl" v="Disabled" />
    <fld n="cpfailAsrtLogCtrl" v="Enabled" />
    <fld n="cpExcepRptCtrl" v="Disabled" />
    <fld n="pcmdEvdoEnable" v="Disabled" />
    <fld n="pcmdEvdoNormConn" v="Disabled" />
    <fld n="pcmdEvdoNormSess" v="Disabled" />
    <fld n="pcmdEvdoRUM" v="Enabled" />
   <fld n="pcmdEvdoFlow" v="Enabled" />
    <fld n="revASLKey" v="1025693969d79fdef19d10c7bb1e794f4f42" />
    <fld n="revAActFlag" v="Activate" />
    <fld n="noOfRevASLAvailable" v="8000" />
   <fld n="noOfRevASLInUse" v="122" />
   <fld n="maxDOSPayload" v="7777" />
    <fld n="enable65KUATISessionsPerOHM" v="Yes" />
    <fld n="enable128KUATISessionsPerOHM" v="No" />
    <fld n="auxRNCCtrl1" v="0" />
    <fld n="orcaOMCRANenabled" v="Yes" />
    <fld n="auxRNCCtrl2" v="0" />
    <fld n="auxRNCCtrl3" v="0" />
    <fld n="auxRNCCtrl4" v="0" />
    <fld n="intraRNCUATIBalanceEnable" v="No" />
    <fld n="intraRNCUATISessionReptEnable" v="Yes" />
    <fld n="intraRNCUATIBalanceThreshold" v="70" />
    <fld n="uatiOverloadThreshold" v="95" />

The occurance of fields to be updated is repetitive, means the updates should be done to multiple fields that have the same field name. I am just cutting the file short for an example
..
Any script (UNIX/Perl) will do.. Thanks peeps and appreciate your repliesSmilie

Last edited by luna_soleil; 08-26-2010 at 02:18 AM..
# 2  
Old 08-26-2010
Try this,

Code:
awk 'NR==FNR{a[$1]=$2;next} { if ($2 in a) {print $1 FS $2 FS $3 FS a[$2] FS $5} else {print $0}}' source.txt FS="\"" datafile.txt

Or

awk 'NR==FNR{a[$1]=$2;next} {if ($2 in a) {$4= a[$2]}}1' source.txt FS="\"" OFS="\"" datafile.txt


Last edited by pravin27; 08-26-2010 at 02:37 AM..
This User Gave Thanks to pravin27 For This Post:
# 3  
Old 08-26-2010
Code:
awk '
NR==FNR{a[$1]=$2;next}
{for (i in a) if (match($0,i)) $3="v=\"" a[i] "\""}1
' source.txt datafile

This User Gave Thanks to rdcwayx For This Post:
# 4  
Old 08-26-2010
Oh sorry guys, I should be using nawk instead of awk. Pardon my silliness :P.

I will try once again your scripts and let you know the outcome :-)

---------- Post updated at 10:13 PM ---------- Previous update was at 09:57 PM ----------

Hey pravin27 and rdcwayx,

I tried all the 3 scripts and they work like magic!

Code:
echo "Enter filename that you want to modify"
read destfile
nawk 'NR==FNR{a[$1]=$2;next} {if ($2 in a) {$4= a[$2]}}1' source.txt FS="\"" OFS="\"" $destfile > tempfile
mv -f tempfile $destfile

Code:
echo "Enter filename that you want to modify"
read destfile
nawk 'NR==FNR{a[$1]=$2;next} { if ($2 in a) {print $1 FS $2 FS $3 FS a[$2] FS $5} else {print $0}}' source.txt FS="\"" $destfile > tempfile
mv -f tempfile $destfile

Code:
echo "Enter filename that you want to modify"
read destfile
nawk 'NR==FNR{a[$1]=$2;next} {for (i in a) if (match($0,i)) $3="v=\"" a[i] "\""}1' source.txt $destfile > tempfile
mv -f tempfile $destfile

Thank u so much guys for making my life easier. XOXOXO ;-)
# 5  
Old 08-26-2010
Code:
#!/bin/bash
IFS=$'\n'
for i in $(sed "" source)
 do
   x=$(echo "$i" | sed 's/ .*$//')
   y=$(echo "$i" | sed 's/^.* //')
sed -i "/$x/s/.*/<fld n=\"$x\" v=\"$y\" \/\>/" infile.txt
 done

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE cmdf SYSTEM "cmdf.dtd">
<cmdf release="20" releaseECP="35.0" snId="5" date="201004190430CDT" >
<frame release="20" releaseECP="35.0" n="FMSFrameId" v="2" >
<entitytype n="FMSFrame" >
<entity >
<keyfld n="hdrsnId" v="5" />
<keyfld n="FMSFrameId" v="2" />
<fld n="hdrRouterIPAddress1" v="0.0.0.0" />
<fld n="hdrRouterIPAddress2" v="0.0.0.0" />
<fld n="hdrEthernetSwitch1IPAddress" v="112.13.9.34" />
<fld n="hdrEthernetSwitch2IPAddress" v="112.13.8.245" />
<fld n="subnetMask" v="104" />
<fld n="colorCode" v="2" />
<fld n="hdrcFMSVCVMIPAddress" v="172.27.11.232" />
<fld n="systemIdentifier" v="1" />
<fld n="ta13ReqTimer" v="2.0" />
<fld n="frameType" v="R1SRFrame" />
<fld n="asrtRptCtrl" v="Enabled" />
<fld n="asrtThrotCtrl" v="Enabled" />
<fld n="cpHighOccRptCtrl" v="Disabled" />
<fld n="cpfailAsrtLogCtrl" v="Enabled" />
<fld n="cpExcepRptCtrl" v="Disabled" />
<fld n="pcmdEvdoEnable" v="Disabled" />
<fld n="pcmdEvdoNormConn" v="Disabled" />
<fld n="pcmdEvdoNormSess" v="Disabled" />
<fld n="pcmdEvdoRUM" v="Enabled" />
<fld n="pcmdEvdoFlow" v="Enabled" />
<fld n="revASLKey" v="1025693969d79fdef19d10c7bb1e794f4f42" />
<fld n="revAActFlag" v="Activate" />
<fld n="noOfRevASLAvailable" v="8000" />
<fld n="noOfRevASLInUse" v="122" />
<fld n="maxDOSPayload" v="7777" />
<fld n="enable65KUATISessionsPerOHM" v="Yes" />
<fld n="enable128KUATISessionsPerOHM" v="No" />
<fld n="auxRNCCtrl1" v="0" />
<fld n="orcaOMCRANenabled" v="Yes" />
<fld n="auxRNCCtrl2" v="0" />
<fld n="auxRNCCtrl3" v="0" />
<fld n="auxRNCCtrl4" v="0" />
<fld n="intraRNCUATIBalanceEnable" v="No" />
<fld n="intraRNCUATISessionReptEnable" v="Yes" />
<fld n="intraRNCUATIBalanceThreshold" v="70" />
<fld n="uatiOverloadThreshold" v="95" />


Last edited by ygemici; 08-26-2010 at 03:37 AM..
This User Gave Thanks to ygemici For This Post:
# 6  
Old 08-26-2010
Thanks ygemici, i've tried your code and that works too! Thanks so much for your reply Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to change values in xml file?

I have xml file like below, i want change the values at default-value place of each argument name using shell script. like where argument name= protocol and default-value=tcp, where argument name =port and default-value= 7223, where argument name = username and default-value=test, example ... (12 Replies)
Discussion started by: s1s2s3s4
12 Replies

2. Shell Programming and Scripting

Change values in .conf file with a script

This is my first time posting here...so be gentle. Suppose I have a test.conf file that contains a value such as a IP. I would like to be able to use the Dialog Utility in Linux to allow me to enter the new IP in a Dialog form...and the results get saved into the test.conf file in the place... (4 Replies)
Discussion started by: calahanp
4 Replies

3. Shell Programming and Scripting

How to change certain values in a file

Hi all, i need help to replace certain values in a file. I need the script to check and match the ID and exNum1. if match, values in $3 (file2.txt) need to replace the value for 'START' (file1.txt) for each match. The sample structure is like this:- File1.txt ID P_6 START ... (4 Replies)
Discussion started by: redse171
4 Replies

4. Shell Programming and Scripting

sorting the datafile in an order given in second datafile

Hi, I have two files: first input file is having 7-8 columns, and second data file is like I want to arrange my datafile1 in the order given in second data file, by comparing the seconddatafile with the second column of first file and print the entire line....also if any... (2 Replies)
Discussion started by: CAch
2 Replies

5. UNIX for Dummies Questions & Answers

Iptables to change source IP

Hey everyone! I am trying to configure iptables/netfilter so that all traffic coming from a certain port (8001) has its source ip re-written as a different ip. Can this be done? Basically its a possible workaround for another issue I'm having with a multihomed server. SNMP data is coming in on... (3 Replies)
Discussion started by: msarro
3 Replies

6. Shell Programming and Scripting

How do I change the values in a file?

TRASH_PATH:~/deleted/ MAX_VERSIONS:5 FILE_MAX_SIZE:1024 FOLDER_MAX_SIZE:8096 TRASH_MAX_SIZE:1024 LOG_MAX_SIZE:100 how do i change the value of TRASH_MAX_SIZE to 2040 using the script? the filename is config.ini please advice Use code tags, ty. (5 Replies)
Discussion started by: classic
5 Replies

7. Shell Programming and Scripting

to parse (or grep) a number from a datafile and write it to tab limited file

Hi All, I have a folder that contain 100's of subfolders namely: Main folder -> GHFG - Subfoders ->10 100 234 102 345 .. .. ... (2 Replies)
Discussion started by: Lucky Ali
2 Replies

8. Shell Programming and Scripting

Combine a datafile with Master datafile, emergent!

Hi guys, my supervisor has asked me to solve the problem in 7 days, I've taken 3 days to think about it but couldn't figure out any idea. Please give me some thoughts with the following problem, I have index.database that has only index date: 1994 1995 1996 1997 1998 1999 I have... (6 Replies)
Discussion started by: onthetopo
6 Replies

9. Shell Programming and Scripting

Change the Values in a file

I have a data file. I want to write a shell script that reads a data file and reads position 19 thru 24. if the data in those fields is 002006, than it should change it to 002007. example: hello world hello 002006 hello world hello world hello world hello 002005 hello world hello world... (6 Replies)
Discussion started by: rudoraj
6 Replies

10. UNIX Desktop Questions & Answers

Change time source?

I need to change the time on my SUN box, it is five minutes fast. (4 Replies)
Discussion started by: michelemcdonald
4 Replies
Login or Register to Ask a Question