Need help with a cfg file. (Search and Replace)

(imported topic written by jcsUTSW)

I know I’ve done this before but I can’t find the action I wrote before and cant remember how I did it.

In a nutshell I need to find “BESClient” in a configuration XML file and replace it with “BES Client”

This is the path to the file… “{(value “etcpath” of key “HKLM\SOFTWARE\BBWin” of registry as string & “\BBWin.cfg”)}”

Another question while I have your attention…

In that same file if I wanted to add a new line or several new lines above the line that looks like this “” how would I do that? Actually I guess the help I get from above could help with that… I could just search for “” and replace with

Line One

Line Two

Maybe?

As always any help greatly appreciated…

Thanks

(imported comment written by NoahSalzman)

Here are a few threads that show how to replace or add text to a file.

http://forum.bigfix.com/viewtopic.php?id=1483

http://forum.bigfix.com/viewtopic.php?id=1521

http://forum.bigfix.com/viewtopic.php?id=1961

http://forum.bigfix.com/viewtopic.php?id=2023

http://forum.bigfix.com/viewtopic.php?id=2147

(imported comment written by jcsUTSW)

Ok so I’ve got the line replacement down…

What if I needed to replace a small section of text… 5 or 6 lines.

For example… This is a piece of a configuration file.

I need to replace everything beween and with lines that are similar but have different values.

I cant just search for warnlevel= because there are many instances of that in this config file so I think the best way would be to edit the file, find lines between and nuke them all and put the new lines back.

As always, your help is greatly appreciated…

(imported comment written by jessewk)

It gets a little tough to do what you want strictly in relevance, but it is possible:

createfile until __ENDCREATE
{ concatenation “%0D%0A” of
(
if
(
line number of item 2 of it > item 0 of it
AND
line number of item 2 of it < item 1 of it
)
then
(
(
if
(
line number of item 2 of it = item 1 of it - 1
)
then
(
(
“new line 1” ; “new line 2”; “new line 3”
)
)
else
(
nothing
)
)
)
else
(
item 2 of it
)
)
of
(
line number of line
whose
(
it starts with “”
)
of it, line number of lines
whose
(
it starts with “”
)
of it, lines of it
)
of file “c:\foo.txt”}
__ENDCREATE

Basically I create a tuple in the form (start line number, end line number, file lines) and then I go through each file line. If the line number is not between the delimiters, then just return the line. If the line number is between the delimiters then throw it away, unless it’s the the line right before the end delimiter. If it’s the line just before the delimiter, replace it with a list of the new lines. Finally concatenate all the lines together with CRLF.

Jesse

(imported comment written by jcsUTSW)

Completed createfile until __ENDCREATE

Failed { concatenation “%0D%0A” of

(

if

(

line number of item 2 of it > item 0 of it

AND

line number of item 2 of it < item 1 of it

)

then

(

(

if

(

line number of item 2 of it = item 1 of it - 1

)

then

(

(

“” ; “”; “”

)

)

else

(

nothing

)

)

)

else

(

item 2 of it

)

)

of

(

line number of line

whose

(

it starts with “”

)

of it, line number of lines

whose

(

it starts with “”

)

of it, lines of it

)

of file “c:\foo.txt”}

__ENDCREATE

/shrug

any ideas?

(imported comment written by jessewk)

yeah.

  1. change the file path at the end so it points at your file instead of “c:\foo.txt”

  2. escape the quotes in the xml attribute values: “”

Jesse

(imported comment written by jcsUTSW)

Still failing… I tried replacing the quotes with %22.

As far as the foo.txt I made a copy of the config file and just copied it there for testing purposes… Once the edit works then I can swap in the path to the actual file…

I appreciate the help… Any other ideas?

(imported comment written by jessewk)

It works for me. This is my action script:

createfile until __ENDCREATE
{concatenation “%0D%0A” of (if (line number of item 2 of it > item 0 of it AND line number of item 2 of it < item 1 of it) then ((if (line number of item 2 of it = item 1 of it - 1) then (("" ; “”; “”)) else (nothing))) else (item 2 of it)) of (line number of line whose (it starts with “”) of it, line number of lines whose (it starts with “”) of it, lines of it) of file “c:\foo.txt”}
__ENDCREATE

Here’s the source file c:\foo.txt:

pre
stuff

post stuff

Here is the output in __createfile

pre
stuff

post stuff

Note, there is a double ‘’ line because you included it in your list of subtituion lines. You probably want to remove that.

(imported comment written by jcsUTSW)

createfile until __ENDCREATE
{concatenation “%0D%0A” of (if (line number of item 2 of it > item 0 of it AND line number of item 2 of it < item 1 of it) then ((if (line number of item 2 of it = item 1 of it - 1) then (("" ; “”;“blah blah blah blah”; “”)) else (nothing))) else (item 2 of it)) of (line number of line whose (it starts with “”) of it, line number of lines whose (it starts with “”) of it, lines of it) of file “c:\foo.txt”}
__ENDCREATE

That’s the action… It says it’s completing just fine now but there are no edits to the file.

I’ve made sure that c:\foo.txt is there and I’ve put only the to section in there and it’s not editing…

/shrug

Any ideas?

Thanks for the continued help.

(imported comment written by jessewk)

The file that gets created will be called __createfile. You need to delete the original and then move the file back in place. Full action:

createfile until __ENDCREATE
{concatenation “%0D%0A” of (if (line number of item 2 of it > item 0 of it AND line number of item 2 of it < item 1 of it) then ((if (line number of item 2 of it = item 1 of it - 1) then (("" ; “”;“blah blah blah blah”; “”)) else (nothing))) else (item 2 of it)) of (line number of line whose (it starts with “”) of it, line number of lines whose (it starts with “”) of it, lines of it) of file “c:\foo.txt”}
__ENDCREATE

delete c:\foo.txt
move __createfile c:\foo.txt

If that’s not the problem, then what do you see when you evaluate the expression in the relevance debugger?