Find and replace Multiple lines in a file

Im trying to find and replace 2 variables in a file with 2 new variables.
There is a configuration file in text called connstore.dat that contains 2 URLs that have to be replaced.

The Single version of the following works but getting it to find and replace both is throwing me. I am getting a “The expression contained a character which is not allowed” error in the appendfile section of the QNA debugger…

parameter “textToReplace1” = “https://site.vt.edu/all

parameter “textToReplace2” = “https://site.vt.edu/vt

parameter “newtext1” = “https://site.vt.edu/all2fa

parameter “newtext2” = “https://site.vt.edu/vt2fa

waithidden “C:\Program Files (x86)\Common Files\Pulse Secure\Integration\pulselauncher.exe” -stop
waithidden net stop PulseSecureService

//OS:win

if { name of operating system as string as lowercase contains “win”}

parameter “filename-win” = “C:\ProgramData\Pulse Secure\ConnectionStore\connstore.dat”

// iterate through the file replacing lines as necessary

appendfile {concatenation “%0d%0a” of ( if (it contains (parameter “textToReplace1” of action as string )and (parameter “textToReplace2” of action as string )) then ((preceding text of first (parameter “textToReplace1” of action as string)of it)&(parameter “newtext1” of action as string )&(following text of first(parameter “textToReplace1” of action as string ) & (preceding text of first (parameter “textToReplace2” of action as string)of it)&(parameter “newtext2” of action as string )&(following text of first(parameter “textToReplace2” of action as string ) of it) )) else it ) of lines of file (parameter “filename-win” of action as string)}

// backup the old file

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

// replace with the new file

copy __appendfile “{parameter “filename-win”}”

runhidden "C:\Program Files (x86)\Common Files\Pulse Secure\JamUI\Pulse.exe"
runhidden net start PulseSecureService
runhidden “C:\Program Files (x86)\Common Files\Pulse Secure\Integration\pulselauncher.exe” -stop
runhidden “C:\Program Files (x86)\Common Files\Pulse Secure\JamUI\Pulse.exe”

any thoughts would be appreciated

Hey Pete. There may be a more elegant way to do this, but try this out and let me know if anything doesn’t make sense or work as expected. The applicability relevance should be written as shown below - I am assuming though that the line ends after the url:

exists lines whose (it contains case insensitive regex "https://site.vt.edu/(all|vt)$") of file "C:\ProgramData\Pulse Secure\ConnectionStore\connstore.dat" | False

And the actionscript should be:

parameter "FilePath" = "C:\ProgramData\Pulse Secure\ConnectionStore\connstore.dat"
parameter "BackupPath" = "C:\ProgramData\Pulse Secure\ConnectionStore\connstore.old.dat"

parameter "CurrentRegexMatch" = "https://site.vt.edu/all|vt"
parameter "FinalRegexMatch" = "https://site.vt.edu/all2fa|vt2fa"

parameter "TextToReplace1" = "https://site.vt.edu/all"
parameter "TextToReplace2" = "https://site.vt.edu/vt"

parameter "NewText1" = "https://site.vt.edu/all2fa"
parameter "NewText2" = "https://site.vt.edu/vt2fa"

// Iterate through the file replacing lines as necessary
appendfile {concatenation "%0d%0a" of (if (it contains case insensitive regex (parameter "TextToReplace1")) then ((parameter "NewText1")) else (if (it contains case insensitive regex (parameter "TextToReplace2")) then ((parameter "NewText2")) else it) ) of lines of file (parameter "FilePath")}

// Backup the old file
delete "{parameter "BackupPath"}"
move "{parameter "FilePath"}" "{parameter "BackupPath"}"

// Replace with the new file
move __appendfile "{parameter "FilePath"}"

// If the new file contains the 2 modified lines as expected, delete the backup file. Otherwise something went wrong, so restore the backup.
if {(exists file (it) and (number of lines whose (it contains case insensitive regex (parameter "FinalRegexMatch")) of file (it)) = 2) of (parameter "FilePath")}
	delete "{parameter "BackupPath"}"
else
	delete "{parameter "FilePath"}"
	move "{parameter "BackupPath"}" "{parameter "FilePath"}"
endif
1 Like

Not sure that “if” condition is what you want
(it contains (parameter "textToReplace1" of action as string ) and (parameter "textToReplace2" of action as string ))
Should those be “and” ? That looks like it’s trying to do ‘bool AND string’ which won’t work. Maybe
(it contains (parameter "textToReplace1" of action as string ) or it contains (parameter "textToReplace2" of action as string ))

That’s still overly complicated thought - still need to make all the replacements plural, since (I presume) a line is expected to have 0 or 1 (but not both) of the strings to replace? But then the “&” concatenation will choke on plural values. So probably
{concatenation "%0d%0a" of (concatenation (parameter "newtext1") of substrings separated by (parameter "textToReplace1") of concatenation (parameter "newtext2") of substrings separated by (parameter "textToReplace2") of it) of lines of file (parameter "filename-win")}

@sean Thanks for the reply, its much appreciated…
The exact syntax of the lines Im searching for is
uri: “https://site.vt.edu/vt” and
uri: "https://site.vt.edu/all"
note the quotes…
Plugging your relevance into qna Im getting a syntax error that points to the | .
If I it out, I get a false answer rather than a true but that was because of me not giving you all the facts… SO

exists lines whose (it contains case insensitive regex “uri:+ %22https://site.vt.edu/(vt|all)%22$”) of file “C:\ProgramData\Pulse Secure\ConnectionStore\connstore.dat” seems to work

Thank you all very much…

1 Like

You could to do the 2 replacements as separate if/then/else blocks in the part that iterates all your lines.

concatenation "%0d%0a" of 
(
 if (it contains (parameter "textToReplace1" of action as string )) 
then ((preceding text of first (parameter "textToReplace1" of action as string)of it)&(parameter "newtext1" of action as string )&(following text of first(parameter "textToReplace1" of action as string ) )) 
else 
(
if (it contains (parameter "textToReplace2" of action as string )) 
then ((preceding text of first (parameter "textToReplace2" of action as string)of it)&(parameter "newtext2" of action as string )&(following text of first(parameter "textToReplace2" of action as string ) )) 
else (it)
)
)

 of lines of file (parameter "filename-win" of action as string)

I’ve used a VBScript for this in the past. The script you can get here.

Then you can use your method of choice to string together the replacements. For example:
waithidden cmd.exe /C CScript C:\PATH_TO_SCRIPT "//B" C:\PATH_OF_FILE_TO_SEARCH STRING_TO_SEARCH_FOR STRING_TO_REPLACE_WITH

you could also use the concat function to make a batch and run it as you were trying above.