需要使用PowerShell将txt文件转换为CSV(带标题)

时间:2017-07-28 11:49:41

标签: powershell export-to-csv

我们可以将下面的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 '

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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 =' ^ ####'