Jump to content

_ArrayTrim


Recommended Posts

Hi all,

I have a text file that has an array of IP addresses followed by the hostnames, like so:

10.20.0.1 ACB-HOST01

10.20.0.2 ACB-HOST02

10.20.0.3 ACB-HOST03

My script can search and locate the IP address, but I need to copy only the hostname to the clipboard, so I can rename the computer hostname and description. How can I trim the array, so that only the hostname is copied to the clipboard?

Thanks!

Link to comment
Share on other sites

Hi all,

I have a text file that has an array of IP addresses followed by the hostnames, like so:

10.20.0.1 ACB-HOST01

10.20.0.2 ACB-HOST02

10.20.0.3 ACB-HOST03

My script can search and locate the IP address, but I need to copy only the hostname to the clipboard, so I can rename the computer hostname and description. How can I trim the array, so that only the hostname is copied to the clipboard?

Thanks!

Many ways to skin that cat. Here's one:
#include <Array.au3> ; Only for _ArrayDisplay()

Global $avTest[3] = ["10.20.0.1 ACB-HOST01", _
    "10.20.0.2 ACB-HOST02", _
    "10.20.0.3 ACB-HOST03"]

_ArrayDisplay($avTest, "Before")

For $n = 0 to UBound($avTest) - 1
    $avTest[$n] = StringTrimLeft($avTest[$n], StringInStr($avTest[$n], " "))
Next

_ArrayDisplay($avTest, "After")

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Getting there. my only problem is that I don't want to define them in the script directly for reasons that the IP list will change over time. Here is what I have:

$file = FileOpen("C:\dhcp\list.txt", 0)
$IPAddress = String(@IPAddress1)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    Select
        Case @error = -1
            MsgBox (0, "Search Result", "End Of File was met")
            ExitLoop
        Case StringInStr($line, @IPAddress1)

            MsgBox (0, "Search Result", $line)
            ExitLoop
    EndSelect
WEnd

FileClose($file)

This works to a point of displaying the entire line that matches the IP address. Now I need to trim the fat to get to the hostname.

Link to comment
Share on other sites

Something like:

$stdin = FileOpen("C:\dhcp\list.csv",0)
$stdout = FileOpen("C:\dhcp\listout.csv",2)
$host = String(@IPAddress1)

while 1
    $line = FileReadLine($stdin)
    if @error = -1 then ExitLoop
    if StringInStr($line,$host) Then
        $split = StringSplit($line, '\,', 0)
        FileWriteLine($stdout,$split[2])
        MsgBox(0,"Found",$host & " found:" & @crlf & $line & @CRLF & "result has been added to results.csv")
    EndIf
WEnd
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...