scraps Posted September 13, 2011 Share Posted September 13, 2011 (edited) Hi people, I am a Novice with Autoit so please bare with me and my lack of knowledge in this area. I have been trying to track down a solution, by searching forums and the help file for the efficient way of extracting certain elements from a text file generated from netsh.exe command. the elements are the IP addresses. the IP addresses start on the 6th line and could stop on the 8-50th line. If I could some how write to array each line starting from 6 and stop when a line contains string total. but only capture the first element of each line within those parameters. the text file: ============================================================================== Scope Address - Subnet Mask - State - Scope Name - Comment ============================================================================== 77.44.107.1 - 255.255.254.0 -Active -test02- 67.108.97.16 - 255.255.255.0 -Active -test01- Total No. of Scopes = 2 Command completed successfully. My broken code thus far (this doesn't work I know still working on it) $sNetshcmd = RunAs($sUserName, $sDomain, $sPwd, 0, "cmd /c " & $sCmd, @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD) While True $data &= StdoutRead($sNetshcmd) If @error Then ExitLoop Sleep(25) WEnd ;FileWrite($sFile, $data) ;ProcessClose($sNetshcmd) for $i = 1 to 100 $sString = FileReadLine($sNetshcmd, $i) if $sString = "Total" then ExitLoop FileWriteLine($sFile, 1) Next Run("notepad.exe " & $sFile) EndFunc ;==>_proc1 Func _proc2() _FileReadToArray($sFile, $dhcpScopeList) while 1 _FileWriteFromArray($sFile, $dhcpScopeList, 6, 0) if $sString = StringInStr FileClose($sFile) Run("notepad.exe " & $sFile) EndFunc ;==>_proc2 If someone could please point me in the right direction and possibly a small example to get me started, it would be greatly appreciated. thank you. Edited September 13, 2011 by scraps Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted September 13, 2011 Moderators Share Posted September 13, 2011 scraps, Welcome to the AutoIt forum. I would do it this way: #include <File.au3> Global $aLines ; Read the file into an array _FileReadToArray("test.txt", $aLines) ; And loop through the array For $i = 1 To $aLines[0] ; Skip the initial lines that do not start with a number If Not Number(StringLeft($aLines[$i], 1)) Then ContinueLoop ; Once we have found the first number then end at the first blank line following If $aLines[$i] = "" Then ExitLoop ; Extract the first IP from the lines $sFound = StringRegExpReplace($aLines[$i], "(.*)\x20\-\x20.*", "$1") ; And display it ConsoleWrite("Found - " & $sFound & @CRLF) NextThe StringRegExpReplace is a bit advanced, it basically does the following:(.*) - Capture all the characters until \x20\-\x20 - we find "space-dash-space" followed by .* - other characters $1 - and replace all that with the captured groupAll clear? 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...
UEZ Posted September 13, 2011 Share Posted September 13, 2011 (edited) Here another regex method: #include <Array.au3> $cmd_input = "==============================================================================" & @LF & _ "Scope Address - Subnet Mask - State - Scope Name - Comment" & @LF & _ "==============================================================================" & @LF & @LF & _ "77.44.107.1 - 255.255.254.0 -Active -test02- " & @LF & _ "67.108.97.16 - 255.255.255.0 -Active -test01- " & @LF & @LF & _ "Total No. of Scopes = 2" & @LF & _ "Command completed successfully." & @LF $aIPInfo = StringRegExp($cmd_input, "(?U)(\d+\.\d+\.\d+\.\d+)\s.*-\s.*(\d+\.\d+\.\d+\.\d+).*\n", 3) If Not @error Then _ArrayDisplay($aIPInfo) It will create an array with following information: even rows: ip address odd rows: subnet mask Br, UEZ Edited September 13, 2011 by UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
scraps Posted September 14, 2011 Author Share Posted September 14, 2011 Thanks Melba23 and UEZ, will let you know how I have progressed with the solutions you have provided. Link to comment Share on other sites More sharing options...
scraps Posted September 16, 2011 Author Share Posted September 16, 2011 Hi Again, I am working in stages with this, and have it working up to a point (will post code once have final draft) , I also needed to incorporate StringStripWS, so the IP addresses were the only data in the output file. Was wondering what is the function to print to screen a single array element? to be saved in a text file. so If i find a value of an array element [140] save the value of the element to text file? #include <Array.au3> #include <File.au3> local $avArray _FileReadToArray("C:\filtered_proc3.txt", $avArray) _ArrayDisplay($avArray, "$avArray2") local $targetHost = "pc005" $sColumn = 1 $iIndex = _ArraySearch($avArray, $targetHost, 0, 0, 0, 1, 1, $sColumn) If @error Then MsgBox(0, "Not Found", '"' & $targetHost & '" was not found on column ' & $sColumn & '.') Else $test = _ArrayToString($iIndex) EndIf MsgBox(0, "", $iIndex) Thanks for your help Link to comment Share on other sites More sharing options...
scraps Posted September 16, 2011 Author Share Posted September 16, 2011 Okay found a way: #include <Array.au3> #include <File.au3> local $avArray _FileReadToArray("C:\filtered_proc3.txt", $avArray) _ArrayDisplay($avArray, "$avArray2") local $targetHost = "PC005" $sColumn = 1 $iIndex = _ArraySearch($avArray, $targetHost, 0, 0, 0, 1, 1, $sColumn) MsgBox(64, "array", $avArray[$iIndex]) FileWriteLine("C:\filtered_proc5.txt", $avArray[$iIndex]) If @error Then MsgBox(0, "Not Found", '"' & $targetHost & '" was not found on column ' & $sColumn & '.') Else $test = _ArrayToString($iIndex) EndIf MsgBox(64, "array", $avArray[$iIndex]) 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