Sign in to follow this  
Followers 0
scraps

help with: txt file to array including only first elements of each line

6 posts in this topic

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

Share this post


Link to post
Share on other sites



scraps,

Welcome to the AutoIt forum. :graduated:

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)

Next

The 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 group

All clear? :)

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

#3 ·  Posted (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 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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Thanks Melba23 and UEZ, will let you know how I have progressed with the solutions you have provided.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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])

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