Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Parsing Powershell:解析结构化文本文件并保存到.CSV_Parsing_Powershell_Text_Csv - Fatal编程技术网

Parsing Powershell:解析结构化文本文件并保存到.CSV

Parsing Powershell:解析结构化文本文件并保存到.CSV,parsing,powershell,text,csv,Parsing,Powershell,Text,Csv,我对Powershell很陌生。只使用了大约2周 我有一个结构如下的文件: Service name: WSDL Service ID: 14234321885 Service resolution path: /gman/wsdlUpdte Serivce endpoints: -------------------------------------------------------------------------------- Service name: DataServi

我对Powershell很陌生。只使用了大约2周

我有一个结构如下的文件:

Service name: WSDL Service ID: 14234321885 Service resolution path: /gman/wsdlUpdte Serivce endpoints: -------------------------------------------------------------------------------- Service name: DataService Service ID: 419434324305 Service resolution path: /widgetDate_serv/WidgetDateServ Serivce endpoints: http://servername.company.com:1012/widgetDate_serv/WidgetDateServ -------------------------------------------------------------------------------- Service name: SearchService Service ID: 393234543546 Service resolution path: /ProxyServices/SearchService Serivce endpoints: http://servername.company.com:13010/Services/SearchService_5_0 http://servername2.company.com:13010/Services/SearchService_5_0 -------------------------------------------------------------------------------- Service name: Worker Service ID: 14187898547 Service resolution path: /ProxyServices/Worker Serivce endpoints: http://servername.company.com:131009/Services/Worker/v9 -------------------------------------------------------------------------------- 服务名称:WSDL 服务编号:14234321885 服务解析路径:/gman/wsdlUpdte 服务端点: -------------------------------------------------------------------------------- 服务名称:DataService 服务ID:419434324305 服务解析路径:/widgetDate\u serv/WidgetDateServ 服务端点: http://servername.company.com:1012/widgetDate_serv/WidgetDateServ -------------------------------------------------------------------------------- 服务名称:SearchService 服务ID:393234543546 服务解析路径:/ProxyServices/SearchService 服务端点: http://servername.company.com:13010/Services/SearchService_5_0 http://servername2.company.com:13010/Services/SearchService_5_0 -------------------------------------------------------------------------------- 服务名称:工人 服务ID:14187898547 服务解析路径:/ProxyServices/Worker 服务端点: http://servername.company.com:131009/Services/Worker/v9 -------------------------------------------------------------------------------- 我希望解析该文件,并在单个列(CSV)中包含服务名称、服务ID、服务解析路径和服务端点(有时包含多个或没有值)

除了使用Get内容和循环文件外,我甚至不知道从哪里开始

任何帮助都将不胜感激。 谢谢

试试这个:

Get-Content | ? { $_ -match ': ' } | % { $_ -split ': ' } | Export-Csv Test.csv;
基本上可以归结为:

  • 以数组形式获取所有文本内容
  • 筛选包含“:”的行
  • 对于剩余的每一行,在“:”上拆分它
  • 将对象数组导出到名为test.CSV的CSV文件
  • 希望这能为你指明正确的方向

    注意:代码未经测试。

    请尝试以下操作:

    Get-Content | ? { $_ -match ': ' } | % { $_ -split ': ' } | Export-Csv Test.csv;
    
    基本上可以归结为:

  • 以数组形式获取所有文本内容
  • 筛选包含“:”的行
  • 对于剩余的每一行,在“:”上拆分它
  • 将对象数组导出到名为test.CSV的CSV文件
  • 希望这能为你指明正确的方向


    注意:代码未经测试。

    这里是解析带有记录和记录记录(等等)的文件的一般方法,它使用带正则表达式的PowerShell开关指令和begin()、Process()、end()函数模板

    加载它,调试它,更正它

    function Parse-Text
    {
      [CmdletBinding()]
      Param
      (
        [Parameter(mandatory=$true,ValueFromPipeline=$true)]
        [string]$ficIn,
        [Parameter(mandatory=$true,ValueFromPipeline=$false)]
        [string]$ficOut
      )
    
      begin
      {
        $svcNumber = 0
        $urlnum = 0
        $Service = @()
        $Service += @{}
      } 
    
      Process 
      {
        switch -regex -file $ficIn
        {
          # End of a service
          "^-+"
          {
            $svcNumber +=1
            $urlnum = 0
            $Service += @{}
          }
          # URL, n ones can exist
          "(http://.+)" 
          {
            $urlnum += 1
            $url = $matches[1]
            $Service[$svcNumber]["Url$urlnum"] = $url
          }
          # Fields
          "(.+) (.+): (.+)" 
          {
            $name,$value = $matches[2,3]
            $Service[$svcNumber][$name] = $value
          }
        }
      }
    
      end 
      {
        #$service[3..0] | % {New-Object -Property $_ -TypeName psobject} | Export-Csv c:\Temp\ws.csv
        # Get all the services except the last one (empty -> the file2Parse is teerminated by ----...----)
        $tmp = $service[0..($service.count-2)] | Sort-Object @{Expression={$_.keys.count };Descending=$true}
        $tmp | % {New-Object -Property $_ -TypeName psobject} | Export-Csv $ficOut
      }
    }
    
    
    Clear-Host
    Parse-Text -ficIn "c:\Développements\Pgdvlp_Powershell\Apprentissage\data\Text2Parse.txt" -ficOut "c:\Temp\ws.csc"
    cat "c:\Temp\ws.csv"
    

    下面是解析带有记录和记录记录的文件(等等)的一般方法,它使用带正则表达式的PowerShell
    switch
    指令和begin()、Process()、end()函数模板

    加载它,调试它,更正它

    function Parse-Text
    {
      [CmdletBinding()]
      Param
      (
        [Parameter(mandatory=$true,ValueFromPipeline=$true)]
        [string]$ficIn,
        [Parameter(mandatory=$true,ValueFromPipeline=$false)]
        [string]$ficOut
      )
    
      begin
      {
        $svcNumber = 0
        $urlnum = 0
        $Service = @()
        $Service += @{}
      } 
    
      Process 
      {
        switch -regex -file $ficIn
        {
          # End of a service
          "^-+"
          {
            $svcNumber +=1
            $urlnum = 0
            $Service += @{}
          }
          # URL, n ones can exist
          "(http://.+)" 
          {
            $urlnum += 1
            $url = $matches[1]
            $Service[$svcNumber]["Url$urlnum"] = $url
          }
          # Fields
          "(.+) (.+): (.+)" 
          {
            $name,$value = $matches[2,3]
            $Service[$svcNumber][$name] = $value
          }
        }
      }
    
      end 
      {
        #$service[3..0] | % {New-Object -Property $_ -TypeName psobject} | Export-Csv c:\Temp\ws.csv
        # Get all the services except the last one (empty -> the file2Parse is teerminated by ----...----)
        $tmp = $service[0..($service.count-2)] | Sort-Object @{Expression={$_.keys.count };Descending=$true}
        $tmp | % {New-Object -Property $_ -TypeName psobject} | Export-Csv $ficOut
      }
    }
    
    
    Clear-Host
    Parse-Text -ficIn "c:\Développements\Pgdvlp_Powershell\Apprentissage\data\Text2Parse.txt" -ficOut "c:\Temp\ws.csc"
    cat "c:\Temp\ws.csv"
    
    尝试一下:

  • 将文件内容作为一个字符串读取
  • 将其拆分为81个连字符
  • 在冒号字符上拆分每个拆分的项,并获取最后一个数组项
  • 为每个项目创建新对象

    $pattern = '-'*81  
    $content = Get-Content D:\Scripts\Temp\p.txt | Out-String
    $content.Split($pattern,[System.StringSplitOptions]::RemoveEmptyEntries) | Where-Object {$_ -match '\S'} | ForEach-Object {
    
    $item = $_ -split "\s+`n" | Where-Object {$_}
    
        New-Object PSobject -Property @{
            Name=$item[0].Split(':')[-1].Trim()
            Id = $item[1].Split(':')[-1].Trim()
            ResolutionPath=$item[2].Split(':')[-1].Trim()
            Endpoints=$item[4..($item.Count)]
        } | Select-Object Name,Id,ResolutionPath,Endpoints
    }
    
  • 尝试一下:

  • 将文件内容作为一个字符串读取
  • 将其拆分为81个连字符
  • 在冒号字符上拆分每个拆分的项,并获取最后一个数组项
  • 为每个项目创建新对象

    $pattern = '-'*81  
    $content = Get-Content D:\Scripts\Temp\p.txt | Out-String
    $content.Split($pattern,[System.StringSplitOptions]::RemoveEmptyEntries) | Where-Object {$_ -match '\S'} | ForEach-Object {
    
    $item = $_ -split "\s+`n" | Where-Object {$_}
    
        New-Object PSobject -Property @{
            Name=$item[0].Split(':')[-1].Trim()
            Id = $item[1].Split(':')[-1].Trim()
            ResolutionPath=$item[2].Split(':')[-1].Trim()
            Endpoints=$item[4..($item.Count)]
        } | Select-Object Name,Id,ResolutionPath,Endpoints
    }
    

  • 使用PowerShell 5,您可以使用出色的命令“convertfrom string”

    $template=@'
    Service name: {ServiceName*:SearchService} 
    Service ID: {serviceID:393234543546} 
    Service resolution path: {ServicePath:/ProxyServices/SearchService} 
    Serivce endpoints:
    http://{ServiceEP*:servername.company.com:13010/Services/SearchService_5_0}
    http://{ServiceEP*:servername2.tcompany.tcom:13011/testServices/SearchService_45_0}
    --------------------------------------------------------------------------------
    Service name: {ServiceName*:Worker} 
    Service ID: {serviceID:14187898547} 
    Service resolution path: {ServicePath:/ProxyServices/Worker} 
    Serivce endpoints:
    http://{ServiceEP*:servername3.company.com:13010/Services/SearchService}
    --------------------------------------------------------------------------------
    Service name: {ServiceName*:WSDL} 
    Service ID: {serviceID:14234321885} 
    Service resolution path: {ServicePath:/gman/wsdlUpdte} 
    Serivce endpoints:
    http://{ServiceEP*:servername4.company.com:13010/Services/SearchService_5_0}
    --------------------------------------------------------------------------------
    '@
    
    
    #explode file with template
    $listexploded=Get-Content -Path "c:\temp\file1.txt" | ConvertFrom-String -TemplateContent $template
    
    #export csv 
    $listexploded |select *, @{N="ServiceEP";E={$_.ServiceEP.Value -join ","}} -ExcludeProperty ServiceEP | Export-Csv -Path "C:\temp\res.csv" -NoTypeInformation
    

    使用PowerShell 5,您可以使用出色的命令“convertfrom string”

    $template=@'
    Service name: {ServiceName*:SearchService} 
    Service ID: {serviceID:393234543546} 
    Service resolution path: {ServicePath:/ProxyServices/SearchService} 
    Serivce endpoints:
    http://{ServiceEP*:servername.company.com:13010/Services/SearchService_5_0}
    http://{ServiceEP*:servername2.tcompany.tcom:13011/testServices/SearchService_45_0}
    --------------------------------------------------------------------------------
    Service name: {ServiceName*:Worker} 
    Service ID: {serviceID:14187898547} 
    Service resolution path: {ServicePath:/ProxyServices/Worker} 
    Serivce endpoints:
    http://{ServiceEP*:servername3.company.com:13010/Services/SearchService}
    --------------------------------------------------------------------------------
    Service name: {ServiceName*:WSDL} 
    Service ID: {serviceID:14234321885} 
    Service resolution path: {ServicePath:/gman/wsdlUpdte} 
    Serivce endpoints:
    http://{ServiceEP*:servername4.company.com:13010/Services/SearchService_5_0}
    --------------------------------------------------------------------------------
    '@
    
    
    #explode file with template
    $listexploded=Get-Content -Path "c:\temp\file1.txt" | ConvertFrom-String -TemplateContent $template
    
    #export csv 
    $listexploded |select *, @{N="ServiceEP";E={$_.ServiceEP.Value -join ","}} -ExcludeProperty ServiceEP | Export-Csv -Path "C:\temp\res.csv" -NoTypeInformation
    

    您必须“硬编码”字段,而不必考虑多个URL。谢谢Shay,我需要做一些更改来修复一些错误消息,但现在输出到控制台时效果很好。当我添加“| export csv test.csv”时,最后一个对象(端点)在实际文件中显示为“System.object[]”。我假设需要对该对象做些什么才能使其成为文本?我会开始谷歌搜索,但如果你能回复的话,那就太好了。谢伊,根据你在另一个网站上对别人的回答,找到了答案。将最后一行更改为:}|选择对象名称、Id、解析路径,@{n=“Endpoints”;e={[string]::join(“;”,$。.Endpoints)}再次感谢您@比尔:好极了,这就是我在一次更新中要写的,在v2中你可以使用-join操作符:@{n=“Endpoints”;e={$\ Endpoints-join';}}@JPBlanc是的,我对字段进行了硬编码,但在我的测试中,我对多个URL(端点)没有任何问题。你必须对字段进行“硬编码”,而不必处理多个URL。谢谢Shay,我需要做一些更改来修复一些错误消息,但现在输出到控制台时效果很好。当我添加“| export csv test.csv”时,最后一个对象(端点)在实际文件中显示为“System.object[]”。我假设需要对该对象做些什么才能使其成为文本?我会开始谷歌搜索,但如果你能回复的话,那就太好了。谢伊,根据你在另一个网站上对别人的回答,找到了答案。将最后一行更改为:}|选择对象名称、Id、解析路径,@{n=“Endpoints”;e={[string]::join(“;”,$。.Endpoints)}再次感谢您@比尔:好极了,这就是我在一次更新中要写的,在v2中,你可以使用-join操作符:@{n=“Endpoints”;e={$\ Endpoints-join';}}}@JPBlanc是的,我对字段进行了硬编码,但在我的测试中,我对多个URL(端点)没有问题。这对任何数量的ServiceEP有效吗?或者我需要为一个包含2个以上条目的条目创建模板吗?这是否适用于任意数量的ServiceEP?或者我需要为一个包含2个以上条目的模板创建一个模板吗?