Sign in to follow this  
Followers 0
Williamk123

Extracting Data from 2d array

13 posts in this topic

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.

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 by mikell

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

#7 ·  Posted (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 by mikell

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

Williamk123,

 

I think I have confused people with my example

You certainly have confused me! :D

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._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

#11 ·  Posted (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 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.

Share this post


Link to post
Share on other sites

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 :shifty: .  Have a great weekend guys.

Share this post


Link to post
Share on other sites

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)

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