Jump to content
Sign in to follow this  

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

Recommended Posts

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

============================================================================== - -Active -test02- - -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
;FileWrite($sFile, $data)
for $i = 1 to 100
  $sString = FileReadLine($sNetshcmd, $i)
  if $sString = "Total" then ExitLoop
  FileWriteLine($sFile, 1)
  Run("notepad.exe " & $sFile)
EndFunc   ;==>_proc1
Func _proc2()
_FileReadToArray($sFile, $dhcpScopeList)
while 1
_FileWriteFromArray($sFile, $dhcpScopeList, 6, 0)
if $sString = StringInStr
  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


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)


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? :)


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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:


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

Here another regex method:

#include <Array.au3>
$cmd_input =    "==============================================================================" & @LF & _
                            "Scope Address - Subnet Mask    - State  - Scope Name    - Comment" & @LF & _
                            "==============================================================================" & @LF  & @LF & _
                            " - -Active     -test02-   " & @LF & _
                            " - -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



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

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 & '.')
$test = _ArrayToString($iIndex)
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 & '.')
$test = _ArrayToString($iIndex)
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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...