【问题标题】:select last lines in parsed text选择已解析文本中的最后一行
【发布时间】:2017-10-17 18:53:34
【问题描述】:

感谢阅读

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

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

【问题讨论】:

    标签: powershell parsing powershell-3.0 text-parsing


    【解决方案1】:

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

    【讨论】:

    • 谢谢!,我喜欢你的建议,但我不知道我是否可以以可持续的方式使用它,因为我有不同的信息数组并且使用正则表达式可能很棘手,你有吗有其他想法吗?
    • 好吧,您将不得不根据阵列调整几乎所有答案。我想不出任何方法来解析一个文本文件,它可以容纳任何抛出的数组。
    【解决方案2】:

    如果您决定使用现有脚本作为基础,您可以通过移动输出行并将其嵌套在 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  
    

    【讨论】:

      猜你喜欢
      • 2015-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-09
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多