Network Automation Software We are currently migrating this forum
over to our HelpSystems domain. Please
post all new threads in our new
HelpSystems Community Portal.
Post to the HelpSystems Forum
You are not currently logged on. You must be logged on in order to post. Log on
Or Create a new account
AutoMate Discussion
Decrease font size
Increase font size
Topic Title: reading in file using basic script, find some text, then find and replace first occurence after
Topic Summary:
Created On: 05/11/2010 03:33 AM
Status: Read Only
Linear : Threading : Single : Branch
Search Topic Search Topic
Topic Tools Topic Tools
View similar topics View similar topics
View topic in raw text format. Print this topic.
 05/11/2010 03:33 AM
User is offline View Users Profile Print this message

Author Icon
RodBland
Apprentice (20-49)

Posts: 30
Joined: 04/10/2005

Hi Folks

Similar to this thread:

http://forums.networkautomatio...catid=4&threadid=7287

This time I want to read in a very large xml file (about 65MB) (let's call it products.xml), and also a small comma-delimited .csv file with a pair of values on each line (call it soh.csv). The first value of the csv is the sku, and the second is stock level of the sku. A few sample lines look like this:

AMF,20
BPA,40
ACZ,9

Two nodes of xml which contains the sku name and stock from the xml file looks like this (spaces inserted before and after angle brackets):

< unqpart mfr="IBM" description="1GB" wty="1 Year" memimage="ibm-ddr.jpg" >
< SupplierNo >AMF< /SupplierNo >
< stock >1< /stock >
< /unqpart >
< unqpart mfr="IBM" description="6GB" wty="1 Year" memimage="ibm-bpa.jpg" >
< SupplierNo >BPAF< /SupplierNo >
< stock >49< /stock >
< /unqpart >

The script needs to do this:

1. Read products.xml, and output it to products-new.xml
2. Find a line containg text like this :

< SupplierNo >amf< /SupplierNo >

3. Once this line is found, the xml containing the stock level of this sku will be in a sibling node further down the file, before the next occurrence of the word "supplier" and looks like this:

< stock >1< /stock >

4. Now with this found, the stock level needs to be updated using the corresponding value from the soh.csv file, so that it ends up looking like this, taking the above samples as an example:

< stock >20< /stock >


5. Then the script needs to find the next occurrence of SupplierNo, then find the next stock node, update it, and so on.

With the help of Bart and Jean I understand how to read the file line by line now, and do search and replace text on each line, but I'm not sure how I can find one line of text, then do the search and replace on a line of text after this is found.

Any help greatly appreciated!

cheers, Rod


 Category Survey
 05/11/2010 04:53 AM
User is offline View Users Profile Print this message

Author Icon
Bart van Lagen
Supreme Guru (1000+)

Posts: 1400
Joined: 04/25/2005

I'll give it a try. For XML you should switch to AM7 though!

 Category Survey


-------------------------
Bart van Lagen
ICT Department.
St Jansdal Hospital
Harderwijk, Netherlands
Automate 7.1.1.0 / BPA 7.1.1.0
Windows 2003SP2SRV
 05/11/2010 05:38 AM
User is offline View Users Profile Print this message

Author Icon
Bart van Lagen
Supreme Guru (1000+)

Posts: 1400
Joined: 04/25/2005

Now this one had some more challenge. You'll have to check those lines:

Const strSupplierIdent = "< SupplierNo >"
Const strStockIdent = "< stock >"

Remove the spaces if neccessary.

Hope it works.

 Category Survey



Task Steps: (Right click, Select All, Copy to copy to the clipboard. Steps can then be pasted into the AutoMate Task Builder)


-------------------------
Bart van Lagen
ICT Department.
St Jansdal Hospital
Harderwijk, Netherlands
Automate 7.1.1.0 / BPA 7.1.1.0
Windows 2003SP2SRV
 05/12/2010 04:04 AM
User is offline View Users Profile Print this message

Author Icon
RodBland
Apprentice (20-49)

Posts: 30
Joined: 04/10/2005

thanks Bart - I'm testing this one out and will report back the results.

 Category Survey
 05/12/2010 04:19 AM
User is offline View Users Profile Print this message

Author Icon
RodBland
Apprentice (20-49)

Posts: 30
Joined: 04/10/2005

Bart - this works spectacularly well. Initially I separated out the nodes that contain stock and part numbers into a separate file as it only takes up a small portion of the file right at the bottom. However, leaving the original 1.1 million line file intact the script still completed in around 30 seconds which is a brilliant result.

I would like to make a small tweak to the log file by including the original stock level, then the new one, but I'm not quite sure how. The line in the log file will look something like this:

21:12:39 started processing sku 2XAAB... Old stock level: 2 New Stock level: 19

I expect I'll need to split the strbuffer to obtain the old stock level and assign it to a variable to achieve part of this - I'm just not sure how.

thanks, Rod

 Category Survey
 05/12/2010 04:56 AM
User is offline View Users Profile Print this message

Author Icon
Bart van Lagen
Supreme Guru (1000+)

Posts: 1400
Joined: 04/25/2005

With a small :-) modification:

 Category Survey



Task Steps: (Right click, Select All, Copy to copy to the clipboard. Steps can then be pasted into the AutoMate Task Builder)


-------------------------
Bart van Lagen
ICT Department.
St Jansdal Hospital
Harderwijk, Netherlands
Automate 7.1.1.0 / BPA 7.1.1.0
Windows 2003SP2SRV
 05/12/2010 02:53 PM
User is offline View Users Profile Print this message

Author Icon
RodBland
Apprentice (20-49)

Posts: 30
Joined: 04/10/2005

Hi Bart - excellent!

One last tweak. Sometimes the amount of stock in the csv file is just empty to indicate zero stock. I could manually modify the csv file beforehand and replace these values with zeroes, but I'm wondering if its possible to incorporate an if/then statement in the script itself, so that if the field is empty that it is converted to 0 (zero) instead.

Thanks for all your help with this - most appreciated.

cheers, Rod

 Category Survey
 05/13/2010 04:43 AM
User is offline View Users Profile Print this message

Author Icon
RodBland
Apprentice (20-49)

Posts: 30
Joined: 04/10/2005

Actually I think I might have figured out. Not sure if it's the most elegant solution though:



 Category Survey



Task Steps: (Right click, Select All, Copy to copy to the clipboard. Steps can then be pasted into the AutoMate Task Builder)
 08/09/2012 06:56 PM
User is offline View Users Profile Print this message

Author Icon
RodBland
Apprentice (20-49)

Posts: 30
Joined: 04/10/2005

Hi Guys

I'm sorry to dig up this old thread, but I could not for the life of me see how I can create a new topic in this particular forum so I'm using this one which is based around a similar theme.

Quite some time ago either Bart or JeanDelfosse helped us write a script which adds an element to a large xml file for us. Basically this script looks for the presence of the text
"< stock" and then adds a new line right below that one with < localstock > 0 < /localstock >

I would like to adapt this script so that if it finds the < localstock > element already present within the parent node, that it doesn't add it in again.

An example of an entire section of xml containing the node which this script operates on is below, including an xml snippet. There are about 2000 of these nodes one after the other in the xml file, representing different product details. We have another process which adds in new products to this file, but the new products are missing the < localstock > element and we can't fix the original process.

I'm a bit stumped on how I get the script to read through the entire node first and only add in the < localstock > line if a line starting with "< localstock" doesn't already exist.

Can someone point me in the right direction?

cheers, Rod


 Category Survey



Task Steps: (Right click, Select All, Copy to copy to the clipboard. Steps can then be pasted into the AutoMate Task Builder)
Statistics
18258 users are registered to the AutoMate Discussion forum.
There are currently 0 users logged in.
The most users ever online was 5551 on 01/08/2018 at 11:11 AM.
There are currently 1565 guests browsing this forum, which makes a total of 1565 users using this forum.

FuseTalk Enterprise Edition v4.0 - © 1999-2020 FuseTalk Inc. All rights reserved.

Sitemap Network Automation Software Blog