select 已解析文本中的最后几行

select last lines in parsed text

感谢阅读

我在一个小脚本中工作,它读取 txt 输出并解析信息,

这是用作示例的信息:

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

所以...这是我在堆栈帮助下使用的代码

    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

代码的结果是:

  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

这可以按行解析信息,但问题是计数重复,如果您查看输出,我正在寻找一种方法来仅获取每个设备的最后一行并解析数字不知道为什么阵列保留来自其他设备的 ip 地址,示例:

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

是相同的 "hostname" 信息,但第一行是从以前的设备中捕获 IP 地址。如果您查看示例,每台设备的最后一行都是正确的。

可能会建议您的解析方法的替代方法。将整个文件作为多行字符串读入,然后根据各个记录将其拆分为多行字符串集,并相应地解析每一个。我的解析方法是将每条记录拆分为每一行,然后创建一个哈希表 ($Props) 并将 Hostname 设置为等于数组中的第二项(因为 'Hostname:' 是第一项物品)。然后我处理数组中与 'X = Y' 类型过滤器匹配的任何行,并将项目添加到每个项目的哈希表中。最后,我将哈希表转换为一个对象,以便可以轻松地使用它。

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

这将输出:

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      

如果您决定使用现有脚本作为基础,您可以通过移动输出行并将其嵌套在 If($content[$i] -match "Hostname:") 脚本块中来改进输出,然后在循环完成后输出最终记录.

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

这将输出:

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