A.lacorazza
A.lacorazza

Reputation: 127

select last lines in parsed text

thanks for reading

im working in a small script, that reads a txt output and parse the information,

this is the information to use as example:

Hostname:
xxxx1-CS0,8.1.9-184
Network:
IPv4 Address            = 1.1.1.1.1.1
IPv4 Netmask            = 1.1.1.1.1.4
IPv4 Gateway            = 1.1.1.1.1.5
DNS Servers             = 1.1.1.1.1.1,1.1.1.1.1.12
Hostname:
xxxx2,7.1.80-7
Network:
IPv4 Address            = 2.2.2.2.1
IPv4 Netmask            = 2.2.2.2.3
IPv4 Gateway            = 2.2.2.2.4
DNS Servers             = 2.2.2.2.2,2.2.2.2.3
Hostname:
xxxxx3,8.1.9-184
Network:
IPv4 Address            = 3.3.3.3.3.1
IPv4 Netmask            = 3.3.3.3.3.2
IPv4 Gateway            = 3.3.3.3.3.5
DNS Servers             = 3.3.3.3.3.3,3.3.3.3.3.4
Hostname:
xxxx4,8.1.9-184
Network:
IPv4 Address            = 4.4.4.1
IPv4 Netmask            = 4.4.4.2
IPv4 Gateway            = 4.4.4.3
DNS Servers             = 4.4.4.41,4.4.4.42

So... this is the code that i'm using with help of stack

    Clear-Host

    $info = Get-Content xxxx

    $finalpatch = "xxxx"

   $content = ($info -split "`n") 

    For($i=0;$i -lt $content.count;$i++){

    if($content[$i] -match "Hostname:")
        {
            #"Hostname Information"
            $infohostname = $content[$i+1]
        }

    elseif($content[$i] -match "IPv4 Address")
        {
            #"Ipv4 Address"
            $infoipv4 = ($content[$i]  -split "=")[1]
        }
    elseif($content[$i] -match "IPv4 Netmask")
        {
            #"Netmask Information"
            $infonetmask = ($content[$i] -split "=")[1]
        }
    elseif($content[$i] -match "IPv4 Gateway")
        {
            #"Gateway Information"
            $gatewayinfo = ($content[$i] -split "=")[1]
        }
    if($content[$i] -match "DNS Servers")
        {
           # "DNS Servers Information"
            $dnsinfo = ($content[$i] -split "=")[1] 
        }



        Write-Host $infohostname ,$infoipv4,$infonetmask,$gatewayinfo,$dnsinfo

and the resulf ot the code is this:

  xxxx1-CS0,8.1.9-184  4.4.4.1  4.4.4.2  4.4.4.3  4.4.4.41,4.4.4.42
xxxx1-CS0,8.1.9-184  4.4.4.1  4.4.4.2  4.4.4.3  4.4.4.41,4.4.4.42
xxxx1-CS0,8.1.9-184  4.4.4.1  4.4.4.2  4.4.4.3  4.4.4.41,4.4.4.42
xxxx1-CS0,8.1.9-184  1.1.1.1.1.1  4.4.4.2  4.4.4.3  4.4.4.41,4.4.4.42
xxxx1-CS0,8.1.9-184  1.1.1.1.1.1  1.1.1.1.1.4  4.4.4.3  4.4.4.41,4.4.4.42
xxxx1-CS0,8.1.9-184  1.1.1.1.1.1  1.1.1.1.1.4  1.1.1.1.1.5  4.4.4.41,4.4.4.42
xxxx1-CS0,8.1.9-184  1.1.1.1.1.1  1.1.1.1.1.4  1.1.1.1.1.5  1.1.1.1.1.1,1.1.1.1.1.12
xxxx2,7.1.80-7  1.1.1.1.1.1  1.1.1.1.1.4  1.1.1.1.1.5  1.1.1.1.1.1,1.1.1.1.1.12
xxxx2,7.1.80-7  1.1.1.1.1.1  1.1.1.1.1.4  1.1.1.1.1.5  1.1.1.1.1.1,1.1.1.1.1.12
xxxx2,7.1.80-7  1.1.1.1.1.1  1.1.1.1.1.4  1.1.1.1.1.5  1.1.1.1.1.1,1.1.1.1.1.12
xxxx2,7.1.80-7  2.2.2.2.1  1.1.1.1.1.4  1.1.1.1.1.5  1.1.1.1.1.1,1.1.1.1.1.12
xxxx2,7.1.80-7  2.2.2.2.1  2.2.2.2.3  1.1.1.1.1.5  1.1.1.1.1.1,1.1.1.1.1.12
xxxx2,7.1.80-7  2.2.2.2.1  2.2.2.2.3  2.2.2.2.4  1.1.1.1.1.1,1.1.1.1.1.12
xxxx2,7.1.80-7  2.2.2.2.1  2.2.2.2.3  2.2.2.2.4  2.2.2.2.2,2.2.2.2.3
xxxxx3,8.1.9-184  2.2.2.2.1  2.2.2.2.3  2.2.2.2.4  2.2.2.2.2,2.2.2.2.3
xxxxx3,8.1.9-184  2.2.2.2.1  2.2.2.2.3  2.2.2.2.4  2.2.2.2.2,2.2.2.2.3
xxxxx3,8.1.9-184  2.2.2.2.1  2.2.2.2.3  2.2.2.2.4  2.2.2.2.2,2.2.2.2.3
xxxxx3,8.1.9-184  3.3.3.3.3.1  2.2.2.2.3  2.2.2.2.4  2.2.2.2.2,2.2.2.2.3
xxxxx3,8.1.9-184  3.3.3.3.3.1  3.3.3.3.3.2  2.2.2.2.4  2.2.2.2.2,2.2.2.2.3
xxxxx3,8.1.9-184  3.3.3.3.3.1  3.3.3.3.3.2  3.3.3.3.3.5  2.2.2.2.2,2.2.2.2.3
xxxxx3,8.1.9-184  3.3.3.3.3.1  3.3.3.3.3.2  3.3.3.3.3.5  3.3.3.3.3.3,3.3.3.3.3.4
xxxx4,8.1.9-184  3.3.3.3.3.1  3.3.3.3.3.2  3.3.3.3.3.5  3.3.3.3.3.3,3.3.3.3.3.4
xxxx4,8.1.9-184  3.3.3.3.3.1  3.3.3.3.3.2  3.3.3.3.3.5  3.3.3.3.3.3,3.3.3.3.3.4
xxxx4,8.1.9-184  3.3.3.3.3.1  3.3.3.3.3.2  3.3.3.3.3.5  3.3.3.3.3.3,3.3.3.3.3.4
xxxx4,8.1.9-184  4.4.4.1  3.3.3.3.3.2  3.3.3.3.3.5  3.3.3.3.3.3,3.3.3.3.3.4
xxxx4,8.1.9-184  4.4.4.1  4.4.4.2  3.3.3.3.3.5  3.3.3.3.3.3,3.3.3.3.3.4
xxxx4,8.1.9-184  4.4.4.1  4.4.4.2  4.4.4.3  3.3.3.3.3.3,3.3.3.3.3.4
xxxx4,8.1.9-184  4.4.4.1  4.4.4.2  4.4.4.3  4.4.4.41,4.4.4.42

This works in order to parse the information in lines, but the issue is the duplication of the count, im looking for a way to get only the last line for each device with the numbers parsed, if you look the output don't know why the array keeps the ip address from other device, example:

xxxx4,8.1.9-184  3.3.3.3.3.1  3.3.3.3.3.2  3.3.3.3.3.5  3.3.3.3.3.3,3.3.3.3.3.4

xxxx4,8.1.9-184  4.4.4.1  4.4.4.2  4.4.4.3  4.4.4.41,4.4.4.42

Is the same "hostname" information, but the first line is catching the IP Address from the previous device. and if you look the examples the final line from each device is the correct one.

Upvotes: 0

Views: 106

Answers (2)

TheMadTechnician
TheMadTechnician

Reputation: 36297

If you are determined to use your existing script as a base you can improve your output by moving the output line and nesting it in the If($content[$i] -match "Hostname:") scriptblock, and then outputting the final record after the loop is done.

Clear-Host

$info = Get-Content xxxx

$finalpatch = "xxxx"

$content = ($info -split "`n") 

For($i=0;$i -lt $content.count;$i++){

if($content[$i] -match "Hostname:")
    {
        #Output the previous record
        Write-Host $infohostname ,$infoipv4,$infonetmask,$gatewayinfo,$dnsinfo            

        #"Hostname Information"
        $infohostname = $content[$i+1]
    }

elseif($content[$i] -match "IPv4 Address")
    {
        #"Ipv4 Address"
        $infoipv4 = ($content[$i]  -split "=")[1]
    }
elseif($content[$i] -match "IPv4 Netmask")
    {
        #"Netmask Information"
        $infonetmask = ($content[$i] -split "=")[1]
    }
elseif($content[$i] -match "IPv4 Gateway")
    {
        #"Gateway Information"
        $gatewayinfo = ($content[$i] -split "=")[1]
    }
if($content[$i] -match "DNS Servers")
    {
       # "DNS Servers Information"
        $dnsinfo = ($content[$i] -split "=")[1] 
    }
}

#Output the final record
Write-Host $infohostname ,$infoipv4,$infonetmask,$gatewayinfo,$dnsinfo

This will output:

xxxx1-CS0,8.1.9-184  1.1.1.1.1.1  1.1.1.1.1.4  1.1.1.1.1.5  1.1.1.1.1.1,1.1.1.1.1.12  
xxxx2,7.1.80-7  2.2.2.2.1  2.2.2.2.3  2.2.2.2.4  2.2.2.2.2,2.2.2.2.3  
xxxxx3,8.1.9-184  3.3.3.3.3.1  3.3.3.3.3.2  3.3.3.3.3.5  3.3.3.3.3.3,3.3.3.3.3.4  
xxxx4,8.1.9-184  4.4.4.1  4.4.4.2  4.4.4.3  4.4.4.41,4.4.4.42  

Upvotes: 1

TheMadTechnician
TheMadTechnician

Reputation: 36297

Might a suggest an alternative to your parsing method. Read the entire file in as a multi-line string, then split it into sets of multi-line strings based on the individual records, and parse each of those accordingly. My method of parsing is to split each record to each line, then create a hashtable ($Props) and set the Hostname to equal the second item in the array (since 'Hostname:' is the first item). Then I process any line in the array where it matches a 'X = Y' type filter, and add items to the hashtable for each of those. Lastly I cast the hashtable to an object so that it can be easily worked with.

Clear-Host

$info = Get-Content xxxx -Raw
$Content = $info -split '(?=Hostname:)'|?{$_}
$Content | %{
    $Record = $_ -split '[\r\n]+'
    $Record|?{$_ -match '(.+)=(.+)'}|% -begin {
            $Props=[ordered]@{'Hostname' = $Record[1].trim()}
        } -Process {
            $Props.Add($Matches[1].trim(),$Matches[2].trim())
        } -End {
            [PSCustomObject]$Props
        }
    }|ft

This will output:

Hostname            IPv4 Address IPv4 Netmask IPv4 Gateway DNS Servers
--------            ------------ ------------ ------------ -----------    
xxxx1-CS0,8.1.9-184 1.1.1.1.1.1  1.1.1.1.1.4  1.1.1.1.1.5  1.1.1.1.1.1,1.1.1.1.1.12
xxxx2,7.1.80-7      2.2.2.2.1    2.2.2.2.3    2.2.2.2.4    2.2.2.2.2,2.2.2.2.3
xxxxx3,8.1.9-184    3.3.3.3.3.1  3.3.3.3.3.2  3.3.3.3.3.5  3.3.3.3.3.3,3.3.3.3.3.4
xxxx4,8.1.9-184     4.4.4.1      4.4.4.2      4.4.4.3      4.4.4.41,4.4.4.42      

Upvotes: 2

Related Questions