2016-06-13 34 views
0

我有一個文本文件,該文件列表服務器的信息,如下面:腳本以CSV變換文本文件列表的內容到表文件

server1 
Status: on 
CPU: 4 cores 
Memory: 256GB 
server2 
Status: off 
server3 
Status: on 
CPU: 8 cores 
Memory: 512GB 

我想將它轉換成CSV文件,如下面:

Name Status CPU Memory 
server1 on 4 256 
server2 off  
server3 on 8 512 

我在網上搜索密集,但找不到任何解決方案。如果有人能在這裏提供一些答案,我會很感激。

+0

一般來說,展示你已經嘗試過的東西是個好主意。話雖如此,我已經發布了一個應該適合你的答案。 –

回答

0

此行區分沒有冒號(服務器名)和線中的一個冒號(服務器屬性),並建立每個服務器的數據集,每次重置它得到一個新的服務器名稱的時間。

# For each line in the text file 
Get-Content d:\data.txt | Foreach { 

    if ($_ -notmatch ':') {      # Line is a server name, reset. 
     if ($server) { [pscustomobject]($server) } # Output dataset (skip 1 at beginning). 
     $server = [ordered]@{'Name'=$_.Trim()}  # create new dataset. 
    } else {          # Line is a server property 
     $key, $value = $_.split(':')    # Process line. 
     $server[$key.Trim()] = $value.Trim()  # Add details to dataset. 
    } 

} | Export-Csv D:\data.csv -NoTypeInformation  # Export, without PS Type details. 

編輯:如果文件在最後一個換行符以上版本纔有效。如果文件末尾沒有換行符,邏輯相同,但此時switch在具有冒號和行的行之間切換,並使用ForEach -End {}塊輸出最後一個條目,則這是一個變體。

Get-Content D:\data.txt | ForEach { Switch -Regex ($_) { 
    ':' {$key, $value = $_.Split(':'); $block[$key] = $value; break} 
    default {if ($block) {[pscustomobject]$block}; $block = [ordered]@{'Name'=$_ }} 
}} -End {[pscustomobject]$block} 

(是的,你可以把它攤開到20行,而不是4 ...或-End{}塊添加到原)。

+0

你的解決方案錯過了最後一個服務器,輸出結果只顯示了2個服務器從我的例子...我嘗試了我的完整列表包含97個服務器,輸出只包含96臺服務器,最後一臺服務器丟失了...... – Edwin

+0

@Edwin ahh,我在文件的末尾放了一個換行符(空行),並導致它輸出最後一個服務器。 – TessellatingHeckler

+0

你能解決這個問題嗎?我喜歡你的解決方案,因爲它很簡單也很靈活,因爲我已經用其他類似文件試過了你的代碼,但是有很多更多的信息,它也可以工作,但是輸出中最後一個服務器丟失了。 – Edwin

0

此解決方案假定每行都格式化一致。即,服務器名稱總是在一行上,其他每個屬性都有一個冒號,後面跟着屬性名稱和屬性值之間的空格。這個答案並不假定每個服務器都有三個屬性,CPU,內存和狀態,如示例數據所示。

$data = @() 
$intput = "D:\Working\data.txt" 
$output = "D:\Working\data.csv" 

# Loop over each line in the text file. 
foreach ($_ in Get-Content $intput) { 

    # Get the index of the ':' in the line. 
    $index = $_.IndexOf(":") 

    # If the index is -1 then the line is a server name. 
    # Create a new object and add it to the array. 
    if ($index -eq -1) { 
     $data += New-Object -TypeName PsCustomObject -Property @{ Name = $_; Status = ""; CPU = ""; Memory = "" } 
    } 
    # If the index is not -1 then the line is a property. 
    # Update the property of the last object added to the array. 
    else { 
     $propertyName = $_.Substring(0, $index) 
     $propertyValue = $_.Substring($index + 2, $_.Length - $index - 2) 

     switch ($propertyName) { 
      "CPU" { $data[$data.Length - 1].CPU = $propertyValue } 
      "Memory" { $data[$data.Length - 1].Memory = $propertyValue } 
      "Status" { $data[$data.Length - 1].Status = $propertyValue } 
     } 
    } 
} 

# Export the data to a CSV file. 
$data | Export-Csv $output -NoTypeInformation 
+0

你的假設是正確的,每一行的格式都是一致的。你的解決方案工作正常,除了列沒有按我需要的順序排列,因此我編輯了最後一行,如下所示:'$ data |選擇對象 - 屬性名稱,狀態,CPU,內存| Export-Csv $ output -NoTypeInformation' ...非常感謝你:) – Edwin