Edit Text File - How to?

(imported topic written by itowns91)

Hi,

I need to edit an existing text file named siteinfo.dat as follows:

I have to edit 3 lines within the file. The first is below:

(0) SiteInfo.Dat file for

(please note that the text “TESTSITE” is different on each machine)

The text within the must be changed to the computer name.

The second line that must be changed:

(4) SPS SortPlan Site Name TESTSITE

The TESTSITE text must be changed to computer name. (please note that the text “TESTSITE” is different on each machine)

The last line that must be changed:

(14) NDSS Router IP Address x.x.x.1

Note this line does not always exist. If it does not exist it must be created with the correct IP Address. The IP Address can be found on the line below:

(1) Remote Host IP Address x.x.x.x

however, the IP Address indicated in line (14) must be x.x.x.11 rather than the x.x.x.x that is provided in line (1)

If line (14) does exist an additional 1 must be added to the IP Address making it x.x.x.11

This seems very complicated to me and so I don’t even know where to start. Can someone help me please?

Ivy

(imported comment written by BenKus)

Hey Ivy,

This is a fun challenge… Let’s see what we can do here…

Our basic approach will be to:

  • Iterate through the lines of the file once to get the Remote IP address in line 1 and store that in a variable
  • Iterate again through the lines of the file
  • For each line that matches our criteria, replace the appropriate pieces (using a few “if” statements)
  • concatenate the lines back together and write them out to a new file
  • Replace the old file

If I understand your file properly, it looks something like this (I have filled in the other lines with quotes from the “Weighted Companion Cube” from the game “Portal” in honor of our forum member apparent obsession :slight_smile: ):

Here is my test file:

(0)         SiteInfo.Dat file 

for <TESTSITE> (1)    Remote Host IP Address    99.99.99.99 
"This weighted companion cube will accompany you through the test chamber. Please take care of it." 
"The symptoms most commonly produced by Enrichment Center testing are superstition, perceiving inanimate objects as alive, and hallucinations. The Enrichment Center reminds you that the weighted companion cube will never threaten to stab you and, in fact, cannot speak." (4)    SPS SortPlan Site Name    TESTSITE 
"The Enrichment Center reminds you that the weighted companion cube cannot speak. In the event that the weighted companion cube does speak, the Enrichment Center urges you to disregard its advice." 
"You did it! The weighted companion cube certainly brought you good luck. However, it cannot accompany you for the rest of the test and must, unfortunately, be euthanized." 
"Rest assured that an independent panel of ethicists has absolved the Enrichment Center, Aperture Science employees, and all test subjects for all moral responsibility for the companion cube euthanizing process." 
"While it has been a faithful companion, your companion cube cannot accompany you through the rest of the test. If it could talk - and the Enrichment Center takes this opportunity to remind you that it cannot - it would tell you to go on without it because it would rather die in a fire than become a burden to you." 
"Although the euthanizing process is remarkably painful, 8 out of 10 Aperture Science engineers believe that the companion cube is most likely incapable of feeling much pain." 
"The companion cube cannot remain through the testing. State and local statutory regulations prohibit it from simply remaining here, alone and companionless. You must euthanize it." 
"You euthanised your faithful companion cube more quickly than any test subject on record. Congratulations." 
"There was even going to be a party for you. A big party that all your friends were invited to. I invited your best friend the companion cube. Of course, he couldn't come because you murdered him. All your other friends couldn't come either because you don't have any other friends because of how unlikable you are. Unlikable, it says so here in your personnel file: Unlikable. Liked by no one. A bitter, unlikable loner whose passing shall not be mourned. SHALL. NOT. BE. MOURNED. That's exactly what it says. Very formal. Very official. It also says you're adopted, so that's funny too." (14)    NDSS Router IP Address    88.88.88.1

Here is my actionscript (change the filename to wherever the file is):

// store the file location
parameter “filename” = “C:\siteinfo.dat”

// store the remote IP address from the “remote host ip address” line
parameter “remoteip” = “{(parenthesized part of match (case insensitive regex “remote host ip address\s*(http://\w.+)”) of it) of line whose (exists match (case insensitive regex “remote host ip address\s*(http://\w.+)”) of it ) of file (parameter “filename”)}”

// if the "ndss router ip exists, then add a “1” to it and store it in a variable, otherwise store blank string
parameter “routerip” = “{if (exists lines whose (exists match (case insensitive regex “NDSS Router IP Address\s*(http://\w.+)”) of it ) of file (parameter “filename”)) then (parenthesized part of match (case insensitive regex “NDSS Router IP Address\s*(http://\w.+)”) of line whose (exists match (case insensitive regex “NDSS Router IP Address\s*(http://\w.+)”) of it ) of file (parameter “filename”) ) & “1” else “”}”

// iterate through the file replacing lines as necessary
appendfile {concatenation “%0d%0a” of ( if (it contains “SiteInfo.Dat file for”) then (preceding text of first "SiteInfo.Dat file for " of it) & "SiteInfo.Dat file for " & (computer name) else if (it contains “SPS SortPlan Site Name”) then (preceding text of first "SPS SortPlan Site Name " of it) & "SPS SortPlan Site Name " & (computer name) else if (it contains “NDSS Router IP Address”) then (preceding text of first "NDSS Router IP Address " of it) & "NDSS Router IP Address " & (parameter “routerip”) else it ) of lines of file (parameter “filename”)}

// then if we don’t have the last line, add it:
if {not exists (parameter “routerip”) OR (parameter “routerip”) = “”}
appendfile {"(14) NDSS Router IP Address " & (preceding text of last “.” of parameter “remoteip”) & “.11”}
endif

// backup the old file
move “{parameter “filename”}” “{parameter “filename”}.bak”

// replace with the new file
move __appendfile “{parameter “filename”}”

I tested, but I suggest you test much more… Let me know if I got something wrong or, better yet, let me know if it works…

Ben

(imported comment written by itowns91)

Ben,

Thanks so much for this. It looks like you got the idea completely. Here’s what happened when I deployed it.

The action has been run 1 time, and will not be run again.

The action failed.

Completed: // store the file location

Completed: parameter “filename” = “d:\nrpc\siteinfo.dat”

Completed: // store the remote IP address from the “remote host ip address” line

Failed: parameter “remoteip” = "{(parenthesized part of match (case insensitive regex "remote host ip address\s*(

http://\w.

+)") of it) of line whose (exists match (case insensitive regex "remote host ip address\s*(

http://\w.

+)") of it ) of file (parameter “filename”)}"

// if the "ndss router ip exists, then add a “1” to it and store it in a variable, otherwise store blank string

parameter “routerip” = "{if (exists lines whose (exists match (case insensitive regex "NDSS Router IP Address\s*(

http://\w.

+)") of it ) of file (parameter “filename”)) then (parenthesized part of match (case insensitive regex "NDSS Router IP Address\s*(

http://\w.

+)") of line whose (exists match (case insensitive regex "NDSS Router IP Address\s*(

http://\w.

+)") of it ) of file (parameter “filename”) ) & “1” else “”}"

// iterate through the file replacing lines as necessary

appendfile {concatenation “%0d%0a” of ( if (it contains “SiteInfo.Dat file for”) then (preceding text of first "SiteInfo.Dat file for " of it) & "SiteInfo.Dat file for " & (computer name) else if (it contains “SPS SortPlan Site Name”) then (preceding text of first "SPS SortPlan Site Name " of it) & "SPS SortPlan Site Name " & (computer name) else if (it contains “NDSS Router IP Address”) then (preceding text of first "NDSS Router IP Address " of it) & "NDSS Router IP Address " & (parameter “routerip”) else it ) of lines of file (parameter “filename”)}

// then if we don’t have the last line, add it:

if {not exists (parameter “routerip”) OR (parameter “routerip”) = “”}

appendfile {"(14) NDSS Router IP Address " & (preceding text of last “.” of parameter “remoteip”) & “.11”}

endif

// backup the old file

move “{parameter “filename”}” “{parameter “filename”}.bak”

// replace with the new file

move __appendfile “{parameter “filename”}”

The actual file text that we are working with:


Time last updated: 11/7/2007 5:15:12 PM


(0) SiteInfo.Dat file for


Disclaimer: This file should only be updated through

NRPC Setup by authorized personnel. Do not directly

update this ASCII file!


(1) Remote Host IP Address x.x.x.47

(2) Auto Start Time 8:00:00 PM

(3) NRPC Log File Creation Date 5/4/05 12:28:50 PM

(4) SPS SortPlan Site Name TESTSITE

(5) AFSM100 Files Window Enable/Disable False

(6) DBCS Files Window Enable/Disable True

(7) DIOSS Files Window Enable/Disable False

(8) FSM881 Files Window Enable/Disable False

(9) FSM1000 Files Window Enable/Disable False

(10) MPBCS Files Window Enable/Disable False

(11) SPBS Files Window Enable/Disable False

(12) Reports Add/Remove Site Code to Filename True

(13) Send Reports to NDSS/DCS NDSS

(imported comment written by BenKus)

Hi Ivy,

Strange… it seems to work OK for me…

Try just using the relevance in the Relevance Debugger and see if it works:

(parenthesized part of match (case insensitive regex “remote host ip address\s*(http://\w.+)”) of it) of line whose (exists match (case insensitive regex “remote host ip address\s*(http://\w.+)”) of it ) of file “d:\nprc\siteinfo.dat”

If that works in the relevance debugger, the issue might be related to some strange substring-cast-to-string that might be different in 6.0->7.0, you might try making a small change:

parameter “remoteip” = “{(parenthesized part of match (case insensitive regex “remote host ip address\s*(http://\w.+)”) of it +as string+) of line whose (exists match (case insensitive regex “remote host ip address\s*(http://\w.+)”) of it ) of file (parameter “filename”)}”

Ben

(imported comment written by itowns91)

Thanks Ben… Here’s what I get in the relevance debugger. (note: I’m using a file I copied to my machine which is why the path is different)

Q: (parenthesized part of match (case insensitive regex "Remote Host IP Address\s*(

http://\w.

+)") of it) of line whose (exists match (case insensitive regex "Remote Host IP Address\s*(

http://\w.

+)") of it ) of file “e:\nrpc\siteinfo.dat”

E: Singular expression refers to nonexistent object.

q: exists file “e:\nrpc\siteinfo.dat”

A: True

T: 0.250 ms

I: singular boolean

q: line 10 of file “e:\nrpc\siteinfo.dat”

A: (1) Remote Host IP Address x.x.x.47

T: 0.447 ms

I: singular file line

(imported comment written by BenKus)

Hey Ivy,

Well… that is certainly strange… can you try these and see if we can isolate the problem:

Q: (parenthesized part of match (case insensitive regex “Remote Host IP Address\s*(http://\w.+)”) of it) of "(1) Remote Host IP Address x.x.x.47"
Q: (parenthesized part of match (case insensitive regex “Remote Host IP Address\s*(http://\w.)”) of it) of it whose (exists match (case insensitive regex “Remote Host IP Address\shttp://\w.") of it ) of "(1) Remote Host IP Address x.x.x.47"
Q: (parenthesized part of match (case insensitive regex "Remote Host IP Address\s
(http://\w.)”) of it) of it whose (exists match (case insensitive regex “Remote Host IP Address\shttp://\w.") of it ) of line 10 of file "e:\nrpc\siteinfo.dat"
Q: exists line whose (exists match (case insensitive regex "Remote Host IP Address\s
(http://\w.+)”) of it ) of file “e:\nrpc\siteinfo.dat”

Ben

(imported comment written by itowns91)

The results are below…

Q: (parenthesized part of match (case insensitive regex "Remote Host IP Address\s*(

http://\w.

+)") of it) of “(1) Remote Host IP Address x.x.x.47”

E: Singular expression refers to nonexistent object.

Q: (parenthesized part of match (case insensitive regex "Remote Host IP Address\s*(

http://\w.

)") of it) of it whose (exists match (case insensitive regex "Remote Host IP Address\s*http://\w.

") of it ) of “(1) Remote Host IP Address x.x.x.47”

E: Singular expression refers to nonexistent object.

Q: (parenthesized part of match (case insensitive regex "Remote Host IP Address\s*(

http://\w.

)") of it) of it whose (exists match (case insensitive regex "Remote Host IP Address\s*http://\w.

") of it ) of line 10 of file “e:\nrpc\siteinfo.dat”

E: Singular expression refers to nonexistent object.

Q: exists line whose (exists match (case insensitive regex "Remote Host IP Address\s*(

http://\w.

+)") of it ) of file “e:\nrpc\siteinfo.dat”

A: False

T: 5.660 ms

I: singular boolean

(imported comment written by itowns91)

Downloaded QNA Relevance Debugger 7.0 and it works: :frowning: can we make it work in 6…

Q: (parenthesized part of match (case insensitive regex "Remote Host IP Address\s*(

http://\w.

+)") of it) of “(1) Remote Host IP Address x.x.x.47”

A: x.x.x.47

T: 0.189 ms

I: singular substring

Q: (parenthesized part of match (case insensitive regex "Remote Host IP Address\s*(

http://\w.

)") of it) of it whose (exists match (case insensitive regex "Remote Host IP Address\s*http://\w.

") of it ) of “(1) Remote Host IP Address x.x.x.47”

A: x.x.x.47

T: 0.271 ms

I: singular substring

Q: (parenthesized part of match (case insensitive regex "Remote Host IP Address\s*(

http://\w.

)") of it) of it whose (exists match (case insensitive regex "Remote Host IP Address\s*http://\w.

") of it ) of line 10 of file “e:\nrpc\siteinfo.dat”

A: x.x.x.47

T: 0.790 ms

I: singular substring

Q: exists line whose (exists match (case insensitive regex "Remote Host IP Address\s*(

http://\w.

+)") of it ) of file “e:\nrpc\siteinfo.dat”

A: True

T: 0.919 ms

I: singular boolean

(imported comment written by BenKus)

Hi Ivy,

That is strange… it appears that our

Boost Regex

engine has been updated in between versions of 6.0 and 7.0 and my regex somehow doesn’t work in the old version:

It seems that you can fix it by replacing

(http://\w.+)

with

(http://0123456789.x+)

in all the expressions.

For instance, change the line in the action to:

parameter “remoteip” = “{(parenthesized part of match (case insensitive regex “remote host ip address\s*(http://0123456789.x+)”) of it) of line whose (exists match (case insensitive regex “remote host ip address\s*(http://0123456789.x+)”) of it ) of file (parameter “filename”)}”

Let me know how that works,

Ben

(imported comment written by itowns91)

You are good. We’ve made progress but … a new failure. I want v. 7 :frowning:

The action has been run 1 time, and will not be run again.

The action failed.

Completed: // store the file location

Completed: parameter “filename” = “d:\nrpc\siteinfo.dat”

Completed: // store the remote IP address from the “remote host ip address” line

Completed: parameter “remoteip” = "{(parenthesized part of match (case insensitive regex "remote host ip address\s*(

http://0123456789.x

+)") of it) of line whose (exists match (case insensitive regex "remote host ip address\s*(

http://0123456789.x

+)") of it ) of file (parameter “filename”)}"

Completed: // if the "ndss router ip exists, then add a “1” to it and store it in a variable, otherwise store blank string

Completed: parameter “routerip” = "{if (exists lines whose (exists match (case insensitive regex "NDSS Router IP Address\s*(

http://0123456789.x

+)") of it ) of file (parameter “filename”)) then (parenthesized part of match (case insensitive regex "NDSS Router IP Address\s*(

http://0123456789.x

+)") of line whose (exists match (case insensitive regex "NDSS Router IP Address\s*(

http://0123456789.x

+)") of it ) of file (parameter “filename”) ) & “1” else “”}"

Completed: // iterate through the file replacing lines as necessary

Failed: appendfile {concatenation “%0d%0a” of ( if (it contains “SiteInfo.Dat file for”) then (preceding text of first "SiteInfo.Dat file for " of it) & "SiteInfo.Dat file for " & (computer name) else if (it contains “SPS SortPlan Site Name”) then (preceding text of first "SPS SortPlan Site Name " of it) & "SPS SortPlan Site Name " & (computer name) else if (it contains “NDSS Router IP Address”) then (preceding text of first "NDSS Router IP Address " of it) & "NDSS Router IP Address " & (parameter “routerip”) else it ) of lines of file (parameter “filename”)}

// then if we don’t have the last line, add it:

if {not exists (parameter “routerip”) OR (parameter “routerip”) = “”}

appendfile {"(14) NDSS Router IP Address " & (preceding text of last “.” of parameter “remoteip”) & “.11”}

endif

// backup the old file

move “{parameter “filename”}” “{parameter “filename”}.bak”

// replace with the new file

move __appendfile “{parameter “filename”}”

(imported comment written by BenKus)

Hey Ivy,

I believe that these issues were related to spaces/tabs problem… I have adjusted it so that instead of spaces, tabs (which are “%09”) are used instead…

This should hopefully work for you:

// store the file location
parameter “filename” = “d:\nrpc\siteinfo.dat”

// store the remote IP address from the “remote host ip address” line
parameter “remoteip” = “{(parenthesized part of match (case insensitive regex “remote host ip address\s*(http://0123456789.x+)”) of it) of line whose (exists match (case insensitive regex “remote host ip address\s*(http://0123456789.x+)”) of it ) of file (parameter “filename”)}”

// if the "ndss router ip exists, then add a “1” to it and store it in a variable, otherwise store blank string
parameter “routerip” = “{if (exists lines whose (exists match (case insensitive regex “NDSS Router IP Address\s*(http://0123456789.x+)”) of it ) of file (parameter “filename”)) then (parenthesized part of match (case insensitive regex “NDSS Router IP Address\s*(http://0123456789.x+)”) of line whose (exists match (case insensitive regex “NDSS Router IP Address\s*(http://0123456789.x+)”) of it ) of file (parameter “filename”) ) & “1” else “”}”

// iterate through the file replacing lines as necessary
appendfile {concatenation “%0d%0a” of ( if (it contains “SiteInfo.Dat file for”) then (preceding text of first “SiteInfo.Dat file for " of it) & “SiteInfo.Dat file for <”>” else if (it contains “SPS SortPlan Site Name%09”) then (preceding text of first “SPS SortPlan Site Name%09” of it) & “SPS SortPlan Site Name%09” & (computer name) else if (it contains “NDSS Router IP Address%09”) then (preceding text of first “NDSS Router IP Address%09” of it) & “NDSS Router IP Address%09” & (“routerip”) else it ) of lines of file (parameter “filename”)}

// then if we don’t have the last line, add it:
if {not exists (parameter “routerip”) OR (parameter “routerip”) = “”}
appendfile {"(14)%09NDSS Router IP Address%09" & (preceding text of last “.” of parameter “remoteip”) & “.11”}
endif

// backup the old file
move “{parameter “filename”}” “{parameter “filename”}.bak”
// replace with the new file
move __appendfile “{parameter “filename”}”

Ben

(imported comment written by itowns91)

Oh so close…

That must’ve been it though… tabs rather than spaces. Thanks.

The action has been run 1 time, and will not be run again.

The action failed.

Completed: // store the file location

Completed: parameter “filename” = “d:\nrpc\siteinfo.dat”

Completed: // store the remote IP address from the “remote host ip address” line

Completed: parameter “remoteip” = "{(parenthesized part of match (case insensitive regex "remote host ip address\s*(

http://0123456789.x

+)") of it) of line whose (exists match (case insensitive regex "remote host ip address\s*(

http://0123456789.x

+)") of it ) of file (parameter “filename”)}"

Completed: // if the "ndss router ip exists, then add a “1” to it and store it in a variable, otherwise store blank string

Completed: parameter “routerip” = "{if (exists lines whose (exists match (case insensitive regex "NDSS Router IP Address\s*(

http://0123456789.x

+)") of it ) of file (parameter “filename”)) then (parenthesized part of match (case insensitive regex "NDSS Router IP Address\s*(

http://0123456789.x

+)") of line whose (exists match (case insensitive regex "NDSS Router IP Address\s*(

http://0123456789.x

+)") of it ) of file (parameter “filename”) ) & “1” else “”}"

Completed: // iterate through the file replacing lines as necessary

Completed: appendfile {concatenation “%0d%0a” of ( if (it contains “SiteInfo.Dat file for”) then (preceding text of first “SiteInfo.Dat file for " of it) & “SiteInfo.Dat file for <”>” else if (it contains “SPS SortPlan Site Name%09”) then (preceding text of first “SPS SortPlan Site Name%09” of it) & “SPS SortPlan Site Name%09” & (computer name) else if (it contains “NDSS Router IP Address%09”) then (preceding text of first “NDSS Router IP Address%09” of it) & “NDSS Router IP Address%09” & (“routerip”) else it ) of lines of file (parameter “filename”)}

Completed: // then if we don’t have the last line, add it:

Completed: if {not exists (parameter “routerip”) OR (parameter “routerip”) = “”}

Completed: appendfile {"(14)%09NDSS Router IP Address%09" & (preceding text of last “.” of parameter “remoteip”) & “.11”}

Completed: endif

Completed: // backup the old file

Failed: move “{parameter “filename”}” “{parameter “filename”}.bak”

// replace with the new file

move __appendfile “{parameter “filename”}”

(imported comment written by itowns91)

This works! Thanks so much Ben!

// store the file location

parameter “filename” = “d:\nrpc\siteinfo.dat”

// store the remote IP address from the “remote host ip address” line

parameter “remoteip” = "{(parenthesized part of match (case insensitive regex "remote host ip address\s*(

http://0123456789.x

+)") of it) of line whose (exists match (case insensitive regex "remote host ip address\s*(

http://0123456789.x

+)") of it ) of file (parameter “filename”)}"

// if the "ndss router ip exists, then add a “1” to it and store it in a variable, otherwise store blank string

parameter “routerip” = "{if (exists lines whose (exists match (case insensitive regex "NDSS Router IP Address\s*(

http://0123456789.x

+)") of it ) of file (parameter “filename”)) then (parenthesized part of match (case insensitive regex "NDSS Router IP Address\s*(

http://0123456789.x

+)") of line whose (exists match (case insensitive regex "NDSS Router IP Address\s*(

http://0123456789.x

+)") of it ) of file (parameter “filename”) ) & “1” else “”}"

// iterate through the file replacing lines as necessary

appendfile {concatenation “%0d%0a” of ( if (it contains “SiteInfo.Dat file for”) then (preceding text of first “SiteInfo.Dat file for " of it) & “SiteInfo.Dat file for <”>” else if (it contains “SPS SortPlan Site Name%09”) then (preceding text of first “SPS SortPlan Site Name%09” of it) & “SPS SortPlan Site Name%09” & (computer name) else if (it contains “NDSS Router IP Address%09”) then (preceding text of first “NDSS Router IP Address%09” of it) & “NDSS Router IP Address%09” & (“routerip”) else it ) of lines of file (parameter “filename”)}

// then if we don’t have the last line, add it:

if {not exists (parameter “routerip”) OR (parameter “routerip”) = “”}

appendfile {"(14)%09NDSS Router IP Address%09" & (preceding text of last “.” of parameter “remoteip”) & “.11”}

endif

// backup the old file

delete “{parameter “filename”}.bak”

move “{parameter “filename”}” “{parameter “filename”}.bak”

// replace with the new file

move __appendfile “{parameter “filename”}”