Sign in to follow this  
Followers 0
valfar999

Replacing value in a file using StringRegExpReplace

11 posts in this topic

Hi, there is this file that have many different variables and values in it in the form of (variable=value)

How can I find a variable in that file, and change its value to something different (Similar to sed in Linux)?

Ex: File has this variable and value

IPADDRESS=10.10.10.10

and the name of the file is net.txt

How can I change IPADDRESS to 9.9.9.9 in that file

I tried:

StringRegExpReplace("IPADDRESS=10.10.10.10", "^IPADDRESS=.*$" "IPADDRESS=9.9.9.9")

but I do not know if there is afunction that would find the value in the file and replace it with what StringRegExpReplace returns.

Thank you

Share this post


Link to post
Share on other sites



It's not necessary to choose the hard way  :)

$txt = FileRead("net.txt")
$txt = StringReplace($txt, "IPADDRESS=10.10.10.10", "IPADDRESS=9.9.9.9")
FileWrite("net2.txt", $txt)
ShellExecute("net2.txt")

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Oh, OK...

Maybe like this then ?

$txt = FileRead("net.txt")
$ref = "IPADDRESS"
$newvalue = "9.9.9.9"
$txt = StringRegExpReplace($txt, "(?<=" & $ref & "=)\S+", $newvalue)
FileWrite("net2.txt", $txt)
ShellExecute("net2.txt")

Edit

Difficult to say more without additional infos...

Edited by mikell

Share this post


Link to post
Share on other sites

... or like this :

$sContent = FileRead("file.txt")

$pattern = "(?m)^IPADDRESS=(\d+\.){3}\d+"
$sNewContent = StringRegExpReplace($sContent, $pattern, "IPADRESS=9.9.9.9")

FileWrite("file2.txt", $sNewcontent)

or more complex, to check the IP address format, but only be for the luxury :sweating:

$pattern = "(?xm) ^IPADDRESS=(?<!\d|\d\.) ( (?:(?:  (?:(?:25[0-5]  |  2[0-4]\d  |  1\d{2}  | [1-9]\d  |  \d  )\.){3}   (?:25[0-5]  |  2[0-4]\d  |  1\d{2}  | [1-9]\d  |  \d  )  ) " & _
" | " & _
"(?:  (?:(?:25[0-5]  |  2[0-4]\d  |  [0-1]\d{2}  |  (00)\d  )\.){3}   (?:25[0-5]  |  2[0-4]\d  |  [0-1]\d{2}  |  (?:00)\d) )) )  (?!\d|\.\d)"

Share this post


Link to post
Share on other sites

I will test those and see. The file will contain other variables and different values (hostnames, names, etc), so that it is why I have to use regular expressions. In sed I would do something like

sed -i "s|^IPADDRESS=.*$|19.9.9.9|g" file.txt but I had no idea how to do this in Autoit.

I will post back once I try your examples. Thank you

Share this post


Link to post
Share on other sites

I tried

$txt = FileRead("net.txt") 
$ref = "IPADDRESS" 
$newvalue = "9.9.9.9" 
$txt = StringRegExpReplace($txt, "(?<=" & $ref & "=)\S+", $newvalue) 
FileWrite("net2.txt", $txt)

but every time I run the script it duplicates the whole file content. I tried adding FileOpen/FileClose with different flags (Specially 2), but when I do that it does not work. How can I stop the script from duplicating the whole content of the file whenever I run it?

Thank you

Share this post


Link to post
Share on other sites

I got it working now, but the problem now is that when I add a value between the = sign and the value and expressions does not work.

Ex:  Variable=Value   (Works)

Ex: Variable = Value (Does not work)

BTW, I was reading and writing to the same file, so I just had to close the file, reopen it with fileOpen using the second flag that that did it.

Thank you

Share this post


Link to post
Share on other sites

What exactly does the contents of this file contain? Is it an INI file or something similar?


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#10 ·  Posted

Nah, if it was I woulda used InitRead. It has different variables and values for a config file Im working on

Ex:

Variable1=xyz

Variable2=qwe

Variable3=cwrvb3rb

I have to change the values according to what a user chooses in my GUI, so that is why I have to be able to find the Vriable and change its value depending on that the user chooses. I cannot convert this to ini format because the application I will be using this with does not support that format.

Share this post


Link to post
Share on other sites

#11 ·  Posted

Try with something like this :

$newvalue = "9.9.9.9"

$sContent = FileRead("file.txt")
$hFile = FileOpen("file.txt", 2)

$pattern = "(?im)^IPADDRESS(\h*)=(\h*).*$"
$sNewContent = StringRegExpReplace($sContent, $pattern, "IPADRESS${1}=${2}" & $newvalue)

ConsoleWrite($sNewContent)
FileWrite($hFile, $sNewContent)

FileClose($hFile)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0