2012-07-18 214 views
0

我對Powershell很新。只用了約2周。Powershell:解析結構化的文本文件並保存到.CSV

我有一個結構類似這樣的文件:

 
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 
-------------------------------------------------------------------------------- 

我想解析文件,並有服務名稱,服務標識,服務解析路徑和服務端點(有時包含多個或不值)在個人柱(CSV)。

除了使用Get-Content並遍歷文件,我甚至不知道從哪裏開始。

任何幫助將不勝感激。 感謝

回答

1

這給一試:

  1. 81個連字符
  2. 讀取文件內容作爲一個字符串
  3. 它分割
  4. 拆分冒號字符上的每個拆分項並取最後一個數組項
  5. 創建新對象每個項目

    $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 
    } 
    
+0

你必須「硬編碼」字段,並沒有照顧多個URL的。 – JPBlanc 2012-07-20 15:24:59

+0

謝謝Shay, 我需要做一些修改才能修復一些錯誤信息,但是現在輸出到控制檯時效果很好。當我添加「| export-csv test.csv」時,最後一個對象(Endpoints)在實際文件中顯示爲「System.Object []」。我假設需要對該對象進行某些操作才能使其成爲文本?我會開始使用Google,但如果您可以回覆,那就太棒了。 – Bill 2012-07-20 15:38:16

+0

謝謝,根據你對另一個網站上其他人的回答計算出來。將最後一行更改爲: } | Select-Object Name,Id,ResolutionPath,@ {n =「Endpoints」; e = {[string] :: join(「;」,$ _。Endpoints)}} 再次感謝您! – Bill 2012-07-20 16:01:22

1

試試這個:

Get-Content | ? { $_ -match ': ' } | % { $_ -split ': ' } | Export-Csv Test.csv; 

基本上它歸結爲:

  1. 獲取所有文本內容作爲一個數組
  2. 過濾器用於包含行 ':'
  3. 對於每行都留下來,將它分開':'
  4. 將對象數組導出到CSV文件nam ED test.csv

希望這點你在正確的方向。

注:代碼未經測試。

0

這是一個通用的解析文件記錄和記錄記錄的方法(等等),它使用功能強大的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" 
1

使用PowerShell 5可以用美妙的命令「convertfrom字符串」

[email protected]' 
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