选择已解析文本中的最后一行

时间:2017-10-17 18:53:35

标签: powershell parsing powershell-v3.0 text-parsing

感谢阅读

我正在一个小脚本中工作,它读取一个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

这样做是为了解析行中的信息,但问题是重复计数,我正在寻找一种方法来获取每个设备的最后一行解析数字,如果你看输出don&# 39;不知道为什么数组保存来自其他设备的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

是相同的"主机名"信息,但第一行是从前一个设备捕获IP地址。如果您查看示例,每个设备的最后一行是正确的。

2 个答案:

答案 0 :(得分:2)

可以建议您解析方法的替代方法。以多行字符串形式读取整个文件,然后根据各个记录将其拆分为多行字符串集,并相应地解析每个字符串。我的解析方法是将每条记录拆分到每一行,然后创建一个哈希表($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      

答案 1 :(得分:1)

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

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