2017-07-28 108 views
-2

我們可以將低於txt文件轉換爲在最後添加的CSV格式嗎?需要使用PowerShell將txt文件轉換爲CSV(帶標題)

文本文件:

 
IP Address: 192.168.1.1 
Hostname: 01-any1TEST 
Event Message: Ping 
Alert Status: Down at least 3 min 
Event Time: 17:25:14 
Alert Type: :Windows 2012 Server 
------------------------------------------------------------------------------------- 
IP Address: 192.168.1.2 
Hostname: 02-any2TEST 
Event Message: Ping 
Alert Status: Down at least 4 min 
Event Time: 17:25:40 
Alert Type: :Unix Server 
------------------------------------------------------------------------------------- 
IP Address: 192.168.1.3 
Hostname: 03-any3TEST 
Event Message: Ping 
Alert Status: Down at least 3 min 
Event Time: 17:26:21 
Alert Type: :windows host 
------------------------------------------------------------------------------------- 

CSV文件輸出要求如下:

 
'IP Address','Hostname','Event Message','Alert Status','Event Time','Alert Type' 
'192.168.1.1','01-any1TEST','Ping','Down at least 3 min','17:26:21','Windows 2012 Server ' 
'192.168.1.2','02-any2TEST','Ping','Down at least 3 min','17:26:21','unix host ' 
'192.168.1.3','03-any3TEST','Ping','Down at least 3 min','17:26:21','windows host ' 
+4

它有可能,你有沒有做過自己的嘗試? – arco444

+0

是的。 $ input = Get-Content -path「path \ Desktop \ in.txt」| Select-String -List'Alert Type','IP Address','Hostname','Event Message','Alert Status','Event Time' $ data = $ input [1 ..($ input.Length - 1 )] $ maxLength = 0 –

+0

$ objects = ForEach($ data in $ data){ $ split = $ record -split「\ s {2,} | \ t +」 If($ split.Length -gt $最大長度){$ 最大長度= $ split.Length } $道具= @ {} 對於($ I = 0; $ I -lt $ split.Length; $ I ++){$ props.Add([字符串] ($ I + 1),$分裂[$ i]於) } 新物體-TypeName PSObject -Property $道具 } $標題= [字符串[]](0 .. $最大長度) $ heade rs1 = [String []]('MachineType','EventTime','HostName','IPAddress','AlertMessage','Severity','AlertType','AlertStatus') $ objects | Select-Object $ headers | Export-Csv -NoTypeInformation -Path「path \ out.csv」 –

回答

1

這裏是你可以做的一種方式:

$logFile = "logfile.txt" 

$delimeterPattern = '^####' 
$recordPattern = '^([^:]+): (.+)' 

# how many lines in a record? 
$recordLines = Select-String $delimeterPattern $logFile -List | 
    Select-Object -ExpandProperty LineNumber 

$logContent = Get-Content $logFile 
for ($i = 0; $i -lt $logContent.Count; $i += $recordLines) { 
    $output = New-Object PSObject 
    for ($j = $i; $j -lt $i + $recordLines; $j++) { 
    $logContent[$j] | Select-String $recordPattern | ForEach-Object { 
     $output | Add-Member NoteProperty $_.Matches[0].Groups[1].Value $_.Matches[0].Groups[2].Value 
    } 
    } 
    $output 
} 

要寫入到CSV文件,把上面的腳本,並管道Export-Csv

+0

出現以下錯誤: export-csv:無法將CSV內容追加到以下文件: C:\ output_temp.csv。附加對象沒有對應於以下列的屬性 :。要繼續使用 不匹配的屬性,請添加-Force參數,然後重試 命令。 在線:17 char:12 + $ output | export-csv「$ tempfile」-notypeinformation -append –

+0

如果我添加-force選項仍然低於錯誤: export-csv:無法處理參數,因爲參數「name」的值無效。更改「名稱」參數的值並再次運行該操作。 在行:17字符:12 + $輸出|出口CSV 「$臨時文件」 -notypeinformation -append -Force + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidArgument:(:) [導出-CSV],PSArgumentException + FullyQualifiedErrorId:參數,Microsoft.PowerShell.Commands.ExportCsvCommand –

+0

你需要把上面的腳本文件,例如名稱'getlog.ps1'。運行該腳本,並將腳本的輸出傳輸到CSV;例如'getlog.ps1 | Export-Csv output.csv -NoTypeInformation'。 –

0

我希望這將解決您的問題:

Function Convert2CSV() 
{ 
    param($logFile, $csvFile) 

    "'IP Address','Hostname','Event Message','Alert Status','Event Time','Alert Type'" | Out-File $csvFile 

    get-content $logFile -Delimiter "-------------------------------------------------------------------------------------" | &{ process{ 
     $IsValidMatch=$_ -match "IP Address: (.*)\r\nHostname: (.*)\r\nEvent Message: (.*)\r\nAlert Status: (.*)\r\nEvent Time: (.*)\r\nAlert Type: (.*)\r\n" 
     if($IsValidMatch) 
     { 
      $nextLine="'$($matches[1])','$($matches[2])','$($matches[3])','$($matches[4])','$($matches[5])','$($matches[6])'" | Out-File $csvFile -Append 
     } 
    }} 
} 

最後調用Convert2CSV功能,定義您的日誌文件和輸出文件作爲參數:

Convert2CSV -logFile .\1.txt -csvFile 3.txt 
+0

它只處理文件中的第一條記錄。我們需要在這裏添加一個循環嗎? –

+0

嘗試;)。它將繼續完整的文件。 – Krisz

0
$delimeterPattern = '^--' 
$recordPattern = '^([^:]+): (.+)' 

$logContent = Get-Content "logfile.txt" 

# how many lines in a record? 
$recordLines = ($logContent | Select-String $delimeterPattern | 
Select-Object -First 1).LineNumber 

for ($i = 0; $i -lt $logContent.Count; $i += $recordLines) { 
$output = New-Object PSObject 
for ($j = $i; $j -lt $i + $recordLines; $j++) { 
$logContent[$j] | Select-String $recordPattern | ForEach-Object { 
    $output | Add-Member NoteProperty $_.Matches[0].Groups[1].Value 
$_.Matches[0].Groups[2].Value 
    } 
} 
$output 
} 

更新後的seprator $ delimeterPattern ='^ - '爲$ delimeterPattern ='^ ####'。

+0

你把你的問題你舉的例子源數據使用'-',沒有''#字符 - 所以當然,我最初發布它沒有工作。你必須在你的問題中提供正確的信息。我用'#'更新了我的答案,並且可以將其標記爲答案。 –

+0

同意,完成。 –

相關問題