Williamk123 Posted November 13, 2014 Share Posted November 13, 2014 Hi, Have looked at several examples but still can seem to get this to work. Want a simple script that will go through a file and replace all occurences of what it finds in cloumn 1 of an array with what it finds in column 2 of the same array. Here is what I have. #include <FileConstants.au3> #include <MsgBoxConstants.au3> #include <File.au3> #include <array.au3> $templatefile = @DesktopDir & "ASASetupASAtemplate.txt" $asaconfigfile = @DesktopDir & "ASASetupASAConfig.txt"$asainifile=@DesktopDir & "ASASetupASASettings.ini"$ConfigVariables=@DesktopDir & "ASASetupConfigVariables.txt" FileCopy($templatefile, $asaconfigfile, $FC_OVERWRITE) Local $ConfigArray =FileReadToArray($ConfigVariables) If @error Then MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file " &$ConfigVariables & ". Error is:" & @error) ; An error occurred reading the current script file. Else For $i = 0 To UBound($ConfigArray) - 1 ; Loop through the array. MsgBox($MB_SYSTEMMODAL, "", $ConfigArray[$i]) ; Display the contents of the array. Local $iRetval = _ReplaceStringInFile($asaconfigfile, $ConfigArray[$i], "TestReplace") If $iRetval = -1 Then MsgBox($MB_SYSTEMMODAL, "ERROR", "The pattern could not be replaced in file: " & $asaconfigfile & " Error: " & @error) Exit EndIf Next EndIf The file it is reading the array from "ConfigVariables.txt will lool like this: <hostname>|test.com <domainname>|domain.com <lanip>|8.8.8.8 So basically the script reads the file into a two column array using | as a delimeter. All instances of what it finds in column 1 will be replcaced with column 2. So in the file ASAConfig.txt , <hostname> will be replaced with test.com, <domainname> will be replaced with domain.com Script I have above almost works. Obviously the _ReplaceStringinFile command to use varialbes that relate to the 2 column array. Link to comment Share on other sites More sharing options...
mikell Posted November 13, 2014 Share Posted November 13, 2014 (edited) ? $txt = "<hostname>|test.com" & @crlf & _ "<domainname>|domain.com" & @crlf & _ "<lanip>|8.8.8.8" Msgbox(0,"", $txt) $txt2 = StringRegExpReplace($txt, '(?m)^(.*)\|(.*)$', "$2|$2") Msgbox(0,"", $txt2) Edit Is there a reason to get the same text twice ? Edited November 13, 2014 by mikell Link to comment Share on other sites More sharing options...
JohnOne Posted November 13, 2014 Share Posted November 13, 2014 Pseudo Code. $hFile_With_Words_To_Be_Replaced = FileOpen("path/to/file.txt", 2) $String_of_the_File = FileRead($hFile_With_Words_To_Be_Replaced) $New_String = "" For $i = 0 To UBound($Your_2D_array) -1 $New_String &= StringReplace($String_of_the_File, $Your_2D_array[$i][0], $Your_2D_array[$i][1]) Next FileWrite($hFile_With_Words_To_Be_Replaced, $New_String) FileClose($hFile_With_Words_To_Be_Replaced) AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Williamk123 Posted November 13, 2014 Author Share Posted November 13, 2014 Hi guys, Thanks for your replys. Mike, don't quite get what you are doing. Think you are saying I can load a whole text file as a string and than just do the replacements. Text file I am doing all the replacements on is pretty large, so don't know if that would work. I will see if I can get it to work though. John, I think you are closer to what I am trying to accomplish. So the question I have is how do I create the 2D array to use in the StringReplace command? So I have a text file that the array will be created from. It is called "ConfigVariables.txt" That file looks something like this: <hostname>|test.com < domainname>|domain.com < lanip>|8.8.8.8 Using that text file and using the | as a delimeter I want to create a two column array. I tried this to just create and display the array: $ConfigVariables=@DesktopDir & "\ASASetup\ConfigVariables.txt" Local $ConfigArray Local $delimeter = "|" _FileReadToArray($ConfigVariables,$ConfigArray,$delimeter) _ArrayDisplay($ConfigArray) All in creates is a Col 0 with the contents of each line in the file the delimeter included. Need two columns. Link to comment Share on other sites More sharing options...
jdelaney Posted November 13, 2014 Share Posted November 13, 2014 How about an array of arrays: $fileContent = "<hostname>|test.com" & @CRLF & _ "< domainname>|domain.com" & @CRLF & _ "< lanip>|8.8.8.8" $a = StringSplit($fileContent,@CRLF,3) For $i = 0 To UBound($a)-1 $a[$i] = StringSplit($a[$i],"|",2) ConsoleWrite(_ArrayToString($a[$i]) & @CRLF) Next You can then convert that into a 2d array... but with slightly different loops, there is no need to. IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. Link to comment Share on other sites More sharing options...
JohnOne Posted November 13, 2014 Share Posted November 13, 2014 There are many ways to create a 2D array, but is it essential you have one, you could just use the 1D array. $My_1D_Array = StringSplit(FileRead("ConfigVariables.txt"), "|", 2) $hFile_With_Words_To_Be_Replaced = FileOpen("path/to/file.txt", 2) $String_of_the_File = FileRead($hFile_With_Words_To_Be_Replaced) $New_String = "" For $i = 0 To UBound($My_1D_Array) -2 Step 2 $New_String &= StringReplace($String_of_the_File, $My_1D_Array[$i], $My_1D_Array[$i + 1]) Next FileWrite($hFile_With_Words_To_Be_Replaced, $New_String) FileClose($hFile_With_Words_To_Be_Replaced) Seems like it should work <untested> AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
mikell Posted November 14, 2014 Share Posted November 14, 2014 (edited) This doesn't work ? $txt = FileRead("input.txt") $txt2 = StringRegExpReplace($txt, '(?m)^(.*)\|(.*)$', "$2|$2") FileWrite("result.txt", $txt2) Edit If all you need is an output txt file, the array way is not necessary If I didn't misunderstand the output file should look like this : test.com|test.com domain.com|domain.com 8.8.8.8|8.8.8.8 Edited November 14, 2014 by mikell Link to comment Share on other sites More sharing options...
Williamk123 Posted November 14, 2014 Author Share Posted November 14, 2014 John, Nope did not work. Script ran and wrote to the file, but no changes made. Looks liek it is still a single column array. If I do an _ArrayDisplay it shows: Row|Col 0 [0]|<hostname> [1]| hostname <domainname> [2]| domainname <lanip> [3]| lanip <wanip> [4]| wantip <dhcppool> [5]| dhcppool <dhcpDNS> [6]| dhcpDNS <dhcpWINs> [7]| dhcpWKINs <dhcpdomain> [8]| dhcpdomain <dhcpDNSalt> [9]| dhcpalt <DNSServerDomain> [10]| DNSServerDomain Second colums is still there. Looks like it is creating a new row at each carriage return. I think it should look like this: Row|Col 0|Col 1 [0]<hostname> | hostname [1]<domainname>| domainname [2]<lanip>| lanip [3]<wanip>| wantip [4]<dhcppool>| dhcppool [5]<dhcpDNS>| dhcpDNS [6]<dhcpWINs>| dhcpWINs [7]<dhcpdomain>| dhcpdomain [8]<dhcpDNSalt>| dhcpalt [9]<DNSServerDomain>| DNSServerDomain Mikell, I think you are not quite getting what I am trying to do. Basically I have two files. The "ConfigVairiables" files and the file I want to do replacements in "Asaconfig.txt" So my idea is read the | delimited ConfigVariables file into a 2 column array. The script will search the file and find all the values of column 1 with the value of column 2 of the array. So "<hostname>" will be replaced with hostname , <wanip> would replaced with wanip, etc. In real life the value for <hostname> would be something like "TestASA" and the vaue for <wanip> woudl be something like "66.65.7.89". I think I have confused people with my example. Also not sure if I am using the correct terminology here. Perhaps all I really do need is a 1D array. Does 2D mean there are 2 or more columns in the array? Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted November 14, 2014 Moderators Share Posted November 14, 2014 Williamk123, I think I have confused people with my exampleYou certainly have confused me! How about you post 3 files - the original "ConfigVairiables" and "Asaconfig.txt" plus the modified "Asaconfig.txt" once you have replaced the required elements - so we can see what exactly we have to manage. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Williamk123 Posted November 14, 2014 Author Share Posted November 14, 2014 Ha, I confuse myself sometimes as well . OK so here are some small examples of the files: ConfigVariables.txt file: <hostname> | TestASA <domainname> | TestNet.com <lanip> | 192.168.102.1 <wanip> | 66.54.7.89 <dhcppool> | 192.168.102.2-192.168.102.250 <dhcpDNS> | 192.168.102.3 <dhcpWINs> | 192.168.102.4 <dhcpdomain> | TestDomain.com <dhcpDNSalt> | 66.35.4.1 File with text that needs to be replaced ASAconfig.txt hostname <hostname> domain-name <domainname> enable password password encrypted interface Ethernet0/0 switchport access vlan 2 ! interface Ethernet0/1 ! interface Ethernet0/2 ! interface Ethernet0/3 ! interface Ethernet0/4 ! interface Ethernet0/5 ! interface Ethernet0/6 ! interface Ethernet0/7 ! clear config dhcpd interface Vlan1 security-level 100 ip address <lanip> ! interface Vlan2 security-level 0 ip address <wanip> dhcpd address <dhcpspool> inside dhcpd dns <dhcpdns> interface inside dhcpd wins <dhcpwins> interface inside dhcpd domain <dhcpdomain> interface inside dhcpd option 6 ip <dhcpdnsalt> interface inside My thought is to load the configvariables file into 2 column array and use the column one as search value and column 2 a replacement value. Loop through the array and make replacements in file for each row in array. So finished product will look lke this: hostname TestASA domain-name TestNet.com enable password password encrypted interface Ethernet0/0 switchport access vlan 2 ! interface Ethernet0/1 ! interface Ethernet0/2 ! interface Ethernet0/3 ! interface Ethernet0/4 ! interface Ethernet0/5 ! interface Ethernet0/6 ! interface Ethernet0/7 ! clear config dhcpd interface Vlan1 security-level 100 ip address 192.168.102.1 ! interface Vlan2 security-level 0 ip address 66.54.7.89 dhcpd address 192.168.102.2-192.168.102.250 inside dhcpd dns 192.168.102.3 interface inside dhcpd wins 192.168.102.4 interface inside dhcpd domain TestDomain.com interface inside dhcpd option 6 ip 66.35.4.1 interface inside Perhaps I'm making this way too difficult? Link to comment Share on other sites More sharing options...
Solution jdelaney Posted November 14, 2014 Solution Share Posted November 14, 2014 (edited) Note: you have a typo in your definition, or template...you pick: <dhcppool> | 192.168.102.2-192.168.102.250 dhcpd address <dhcpspool> inside Where difinition.txt is your mappings, and template includes where to put those mappings: Edit: removed the middle man, and now setting directly to $aTemp. $fileContent = FileRead("definition.txt") $fileTemplate = FileRead("template.txt") $a = StringSplit($fileContent,@CRLF,3) For $i = 0 To UBound($a)-1 $aTemp = StringSplit($a[$i],"|",2) $fileTemplate = StringReplace($fileTemplate,StringStripWS($aTemp[0],3),StringStripWS($aTemp[1],3)) Next ConsoleWrite($fileTemplate & @CRLF) output: hostname TestASA domain-name TestNet.com enable password password encrypted interface Ethernet0/0 switchport access vlan 2 ! interface Ethernet0/1 ! interface Ethernet0/2 ! interface Ethernet0/3 ! interface Ethernet0/4 ! interface Ethernet0/5 ! interface Ethernet0/6 ! interface Ethernet0/7 ! clear config dhcpd interface Vlan1 security-level 100 ip address 192.168.102.1 ! interface Vlan2 security-level 0 ip address 66.54.7.89 dhcpd address <dhcpspool> inside dhcpd dns 192.168.102.3 interface inside dhcpd wins 192.168.102.4 interface inside dhcpd domain TestDomain.com interface inside dhcpd option 6 ip 66.35.4.1 interface inside Edited November 14, 2014 by jdelaney IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. Link to comment Share on other sites More sharing options...
Williamk123 Posted November 14, 2014 Author Share Posted November 14, 2014 Yup I made a boo boo. This works perfectly. Now I just have to study it so i understand exactly what you are doing. Looks pretty straight forward though. Thank you much to all who contributed. This would have been a lot easier if my initial post had been more clear. Don't know why I think everyone else thinks exactly like I do and should therefore understand everything I say . Have a great weekend guys. Link to comment Share on other sites More sharing options...
Malkey Posted November 14, 2014 Share Posted November 14, 2014 Instead of jdelaney's StringSplit in a loop to get a 2D array, in your first post you tried to use the FileReadToArray function. And instead of jdelaney's StringReplace you tried to use the _ReplaceStringInFile function. Here is a working version for your first post. #include <FileConstants.au3> #include <MsgBoxConstants.au3> #include <File.au3> #include <array.au3> ;Local $templatefile = @DesktopDir & "\ASAtemplate.txt" Local $asaconfigfile = @DesktopDir & "\ASAConfig.txt" ;Local $asainifile = @DesktopDir & "\ASASettings.ini" Local $ConfigVariables = @DesktopDir & "\ConfigVariables.txt" ;FileCopy($templatefile, $asaconfigfile, $FC_OVERWRITE) Local $ConfigArray, $iRetval _FileReadToArray($ConfigVariables, $ConfigArray, $FRTA_ENTIRESPLIT, " | ") If @error Then MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file " & $ConfigVariables & ". Error is:" & @error) ; An error occurred reading the current script file. Else _ArrayDisplay($ConfigArray, "$ConfigArray") For $i = 0 To UBound($ConfigArray) - 1 ; Loop through the array. $iRetval = _ReplaceStringInFile($asaconfigfile, $ConfigArray[$i][0], $ConfigArray[$i][1]) If $iRetval > 0 Then MsgBox($MB_SYSTEMMODAL, "Success", 'The text "' & $ConfigArray[$i][0] & '" replaced with "' & $ConfigArray[$i][1] & '" ' & $iRetval & " " & " time" & ($iRetval > 1 ? "s" : "")) EndIf Next EndIf ShellExecute($asaconfigfile) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now