我有這個日誌文件,我試圖解析它。 問題是數據線的格式爲「價值」,「價值」,......以及一些次「價值\」價值\「」 ...用雙引號括起來的值解析文本類型的日誌文件,並用逗號分隔
#basepath D:\XHostMachine\Results
#results test.res
#fields TestPlan Script TestCase TestData ErrorCount ErrorText DateTime Elapsed
#delimiter ,
#quote " \
"D:\XHostMachine\plans\test.pln","D:\XHostMachine\testcases\test.t","rt1","1,\"a\"",1,"[#ERROR#][APPS-EUAUTO1] [error] rt1 t1 (Screen shot : D:\XTestMachines\Error\[APPS-EUAUTO1] 03-28-14 11-29-22.png)","2014-03-28 11.29.04","0:00:18"
"D:\XHostMachine\plans\test.pln","D:\XHostMachine\testcases\test.t","rt2","1,\"a\"",0,"","2014-03-28 11.29.22","0:00:08"
,但我不能夠分割使用","
作爲分隔符(因爲,
可能存在的內部)的線
我的代碼是:
Function Get-RexLog {
Param ($File)
# Reads the log file into memory.
Try {
Get-Content -path $File -ErrorAction Stop | select -skip 6 # skips the first 6 lines
} Catch {
Write-Error "The data file is not present"
BREAK
}
} # End: Function Get-RexLog
# -----------------------------------------------------------------------
Function Get-Testplan {
Param ($RexLog)
for ($i=0; $i -lt $RexLog.Count; $i++) {
$Testcase = $RexLog[$i].Split("`"[,]`"") | ForEach-Object - process {$_.TrimStart('"')}
$Output = New-Object PSobject -Property @{
TestPlan = $Testcase[0]
Script = $Testcase[1]
TestCase = $Testcase[2]
TestData = $Testcase[3]
ErrorCount = $Testcase[4]
ErrorText = $Testcase[5]
DateTime = $Testcase[6]
Elapsed = $Testcase[7]
}
}
} # End: Function Get-Testplan
# -----------------------------------------------------------------------
# Parse the files
$RexLog = Get-RexLog -file "D:\XHostMachine\Results\test.rex"
$Testplan = Get-Testplan -RexLog $RexLog
$Testplan
FINAL編輯:使用 ConvertFrom-CSV
ConvertFrom-Csv -inputobject $RexLog -Header @("TestPlan","Script","TestCase","TestData","ErrorCount","ErrorText","DateTime","Elapsed")
這可以很容易地用正則表達式來完成。你想使用正則表達式作爲分裂? – sln
@sln使用正則表達式不會有問題,但我不知道如何實現它。你能給我一個建議嗎?謝謝 – Ionut
我不知道Powershell的正則表達式函數調用,但我可以給你正則表達式。 – sln