Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing a number in an xml-file 1

Status
Not open for further replies.

powallin

Programmer
Apr 22, 2005
2
DK
Hi

I have an xml-file containing multiple records like:

<DPparameter>
<SWid>SCMTYPE</SWid>
<position>500</position>
<size>16</size>
</DPparameter>

All records currently indicate position 500.
What I want to do is replace 500 with 500 + a running number.
I've tried running the code below with nawk:

BEGIN{
count = 0
}
{
newposition = firstposition + count
if(gsub(/500/, newposition)) {
print "Replaced 500 with " newposition
count++
}
}

The variable firstposition is given as input on the command line which looks like:
> nawk -f Replace firstposition=500 XmlTestFile

The output I get looks like the operation has succeded:

Replaced 500 with 500
Replaced 500 with 501
Replaced 500 with 502
Replaced 500 with 503
...etc...

However, nothing has changed in my file.

What am I doing wrong. I just can't find out.
I'm going crazy!

Thanks a lot in advance for your help.

 
nawk -v firstposition=500 '
{ if(gsub(/500/, firstposition)) firstposition++
print
}
' XmlTestFile > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks a lot for that!
I'm a beginner with awk and had missed a rather apparent thing, to direct the output to a new file!

:)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top