【问题标题】:Need help sorting data line by line in powershell需要帮助在powershell中逐行排序数据
【发布时间】:2019-08-09 16:35:37
【问题描述】:

我需要查看 cisco 配置文件并提取特定信息来绘制我们网络的第 3 层图。我需要从配置中提取接口、地址、vlan ID、描述等,并将其排列在表格或excel电子表格中。我在脑海中思考代码的方式是将其设置为以某种方式说“在第 x 行的单词 vlan 和单词 vlan 的下一个实例之间,获取此信息”,然后在单词界面之间调整它,等等,但我不确定如何将其放入代码中。不幸的是,我在 powershell 上相当新

我正在使用的 cisco 配置的一小部分示例。我试图从中得到的是“vlan”行中的 vlan 和编号以及紧接下一行中的全名行。

vlan 2
  name mgmt
vlan 10
  name vmotion_tools
vlan 13
  name perfmontools_10.12.144.24
vlan 14
  name perfmontools_10.12.145.2
etc

这是我目前所拥有的:

foreach($line in [System.IO.File]::ReadLines('....\Config_test.txt'))
    {
        #look for line containing vlan
       if ($line -like '*vlan*'){
        $vlanid = $line
        }
        #look for line containing name
       if ($line -like '*name*'){
        $vlanname = $line
        $vlanname = $vlanname -replace ".*name "    
        }
        #look for line containing IP
       if ($line -like '*ip*'){
        $vlan_ip = $line  
        }

        @{name = $vlanid; description = $vlanname} | Out-String 

    }  

它给出了以下结果:

Name                           Value                                                                                                                                    
----                           -----                                                                                                                                    
name                           vlan 242                                                                                                                                 
description                    perfmontools-172.xxx                                                                                                            


Name                           Value                                                                                                                                    
----                           -----                                                                                                                                    
name                           vlan 242                                                                                                                                 
description                    perfmontools-172.xxx
etc
etc
etc

所以它们至少被正确地组合在一起。我希望名称和描述出现在一行上,就像一张桌子,然后从那里继续下去。当您不知道自己在做什么时,它会很慢:)

这样做的最终目标是将所有这些信息放入 Excel 电子表格中

【问题讨论】:

  • 请贴几行配置文件
  • 配置文件超过 1000 行长,我从获取 vlan 名称和描述开始,但这里是我正在使用 vlan 9 name perfmontools1 vlan 28 name 的小示例perfmontools2 vlan 32 名称 perfmontools 3 依此类推。 (当我点击提交时,格式被提升了。它进入 vlan 9(新行)名称 perfmontools1(新行)vlan 28......
  • edit your post,cmets 对于代码和数据来说太糟糕了:)
  • Get-Content 发出的内容不是具有名称和 ID 等属性的对象。每个对象只是一行文本。如果您的输入是带有标题“名称、ID”的 CSV 文件,您可以轻松使用 Import-CSV。事实上,您将不得不解析您的输入文件。我不愿提供建议,因为您是 Powershell 新手。
  • 在通过 where-object 部分之前是否必须解析文件?我无法导入为 csv 文件

标签: powershell text-parsing


【解决方案1】:

我建议使用带有-File-Regex 参数的switch statement

$infoObjs = switch -Regex -File '....\Config_test.txt' {
    '^vlan ' { $name = $_; continue }
    '^\s+name (.*)' {
       # Output a custom object with the name and the description.
       # $infoObjs will collect them in an array.
       [pscustomobject] @{
            Name = $name
            Description = $Matches[1]
       }
       continue
    }
}

# Now you can send the objects to Export-Csv to create
# a CSV file, for instance:
$infoObjs | Export-Csv -NoTypeInformation info.csv

如果你将$infoObjs 直接输出到控制台,你会看到类似:

Name    Description
----    -----------
vlan 2  mgmt
vlan 10 vmotion_tools
vlan 13 perfmontools_10.12.144.24
vlan 14 perfmontools_10.12.145.2

【讨论】:

    【解决方案2】:

    Install-Module ImportExcel 可以处理 Excel 文件的创建。

    这是一种解析文本的方法。

    $data = @"
    vlan 2
      name mgmt
    vlan 10
      name vmotion_tools
    vlan 13
      name perfmontools_10.12.144.24
    vlan 14
      name perfmontools_10.12.145.2
    "@ -split "`r`n"
    
    $(for ($i = 0; $i -lt $data.count; $i++) {
        [PSCustomObject][Ordered]@{
            Name = $data[$i++]
            Description = $data[$i].Trim() -replace "name ",""
        }
    }) | Export-Excel
    

    【讨论】:

    • 是否有额外的模块来执行截图中嵌入的代码?或者也许是更好的表达方式?
    • 是的,评论顶部的Install-Module ImportExcel,然后它会自动导入。
    • 我的意思是,通过将您的代码作为屏幕截图发布,任何想亲自尝试的人都必须重新输入它,而如果它以文本形式发布,对每个人来说,事情变得更容易、更容易获得。我只是觉得讽刺的是,关于提取文本本身的答案需要文本提取。
    猜你喜欢
    • 2019-12-25
    • 1970-01-01
    • 2019-11-07
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 2011-05-10
    相关资源
    最近更新 更多