我有这个日志文件,我试图解析它。 问题是数据线的格式为“价值”,“价值”,......以及一些次“价值\”价值\“” ...用双引号括起来的值解析文本类型的日志文件,并用逗号分隔
#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