我正在處理以下要求。我有以下輸入檔案并嘗試使用正則運算式提取特定資訊并創建一個包含所有匹配項的輸出 csv 檔案。
示例檔案
[2022-05-31T16:56:25.551558-04:00] [XFM] [TRACE:1] [EPMHFM-00000] [XFM] [ecid: XDS.0000.0000.0000.0001] [File: c:\jenkins\workspace\hfm_11.2.8_rue_build\hfm\source\xfmdatasourceroot\xfmdatasource\xfmdatasource.cpp] [Line: 601] [userId: ] [Msg arguments: ] [appName: COMMAPS4] [pid: 8364] [tid: 14160] [host: win2019standard] [nwaddr: [fe80::3d7e:f4de:2f83:8e30%4]:0;10.65.51.209:0;] [errorCode: 0] [srcException: 0] [errType: 1] [dbUpdate: 2] [11.2.8.1.000.9] [[XDS: XFMDataSource process starting...
[2022-08-28T20:04:39.037507-04:00] [XFM] [TRACE:1] [EPMHFM-00000] [XFM] [ecid: ] [File: c:\jenkins\workspace\hfm_11.2.6_build\hfm\source\xfmdatasourceroot\xfmdatasource\xfmdatasource.cpp] [Line: 489] [userId: ] [Msg arguments: ] [appName: COMMAPS4] [pid: 11496] [tid: 9268] [host: Win2019standard] [nwaddr: [fe80::dca:9652:390a:7031]:0;10.199.36.35:0;] [errorCode: 0] [srcException: 0] [errType: 1] [dbUpdate: 2] [11.2.6.0.000.38] [[XDS: XFMDataSource process exiting now ...
下面是用于提取所需值的帶有正則運算式的 powershell。
Get-Content -Path "C:\Users\test.txt" |select-String -pattern "(\d{4}\-\d{2}\-\d{2})|`(\d\d:\d\d:\d\d|host :\s([a-zA-Z \d] ))|appName :\s([a-zA-Z \d] )|`(11.\d.\d.\d.\d\d\d.\d\d)|pid :\s([a-zA-Z \d] )|XDS :\s([a-zA-Z \d] )"-AllMatches|ForEach-Object {$_.Matches.value} |ForEach-Object {$_.Groups[1].Value}
樣本輸出:
2022-05-31
16:56:25
appName: COMMAPS4
pid: 8364
host: win2019standard
XDS: XFMDataSource process starting
========
目標是使用以下格式創建包含所有結果的 CSV 檔案。
Date , Time , Appname , PID , Host , Message
2022-05-31, 16:56:25, COMMAPS4, 8364,win2019standard, XFMDataSource starting
嘗試了 outfile 和 export-Csv 沒有提供所需的輸出
$content =Get-Content -Path "C:\Users\test.txt"
$regex = '(\d{4}\-\d{2}\-\d{2})|(\d\d:\d\d:\d\d|host :\s([a-zA-Z \d] ))|appName :\s([a-zA-Z \d] )|(11.\d.\d.\d.\d\d\d.\d\d)|pid :\s([a-zA-Z \d] )|XDS :\s([a-zA-Z \d] )'
[regex]::Matches($content, $regex) | % {
[PSCustomObject]@{
Date = $_.Groups.Value[1]
Time = $_.Groups.Value[2]
Server= $_.Groups.Value[3]
Application= $_.Groups.Value[4]
Version= $_.Groups.Value[5]
PID= $_.Groups.Value[6]
Message= $_.Groups.Value[7]
}
} | Export-Csv -Path C:\Users\test.csv
uj5u.com熱心網友回復:
看起來你可以使用這個 regex來完成它。使用有問題的樣本,物件將如下所示:
Date Time AppName PID Host Message
---- ---- ------- --- ---- -------
2022-05-31 16:56:25 COMMAPS4 8364 win2019standard XFMDataSource process starting
2022-08-28 20:04:39 COMMAPS4 11496 Win2019standard XFMDataSource process exiting now
代碼:
$re = [regex] @'
(?xi)
(?<Date>\d{4}(?:-\d{2}){2}).*?
(?<Time>\d{2}(?::\d{2}){2}).*?appName[\s:]*
(?<AppName>[\w ] ).*?pid[\s:]*
(?<PID>\d ).*?host[\s:]*
(?<Host>[\w ] ).*?XDS[\s:]*
(?<Message>[\w ] )
'@
$log = Get-Content path\to\log.txt -Raw
$re.Matches($log) | ForEach-Object {
$out = [ordered]@{}
foreach($group in $_.Groups) {
if($group.Name -eq 0) { continue }
$out[$group.Name] = $group.Value
}
[pscustomobject] $out
} | Export-Csv path\to\export.csv -NoTypeInformation
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/517242.html
標籤:正则表达式电源外壳导出到 csv