我需要計算值為 ms 2xx 的行(其中 xx 是任何數字)它可以是 200,201,202,258,269 等。(它必須從數字 2 開始)然后也用數字 4 和 5 來計算。
在我的 .txt 檔案中有這樣的行:
2022.10.20 13:42:01.570 | INFO | Executed action "PERPIRestEPService.Controllers.PERPIController.GetVersion (PERPIRestEPService)" in 4.9487ms
2022.10.20 13:42:01.570 | INFO | Executed endpoint '"PERPIRestEPService.Controllers.PERPIController.GetVersion (PERPIRestEPService)"'
2022.10.20 13:42:01.570 | INFO | Request finished in 5.5701ms 200 application/json; charset=utf-8
2022.10.20 13:42:01.908 | DBUG | Starting HttpMessageHandler cleanup cycle with 4 items
2022.10.20 13:42:01.908 | DBUG | Ending HttpMessageHandler cleanup cycle after 0.0105ms - processed: 4 items - remaining: 0 items
2022.10.20 13:44:30.632 | DBUG | Received data from rabbit: <?xml version="1.0"?>
<TransactionJournal xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.datapac.sk/Posybe">
<Header>
<GeneratedDate>2022-10-20T13:44:30.5409065 02:00</GeneratedDate>
<BusinessDate>2022-10-20</BusinessDate>
<SourceSystem>Posybe MCFS</SourceSystem>
<Site>C702</Site>
<Version>1.0</Version>
我需要制作這張桌子。它應該是這樣的:
Count Name
----- ----
97 200
278 202
2 205
18 275
我有這段代碼:
$files = Get-ChildItem -Path "C:\Users\krivosik\Desktop\Scripts\logs\PosybeRestEPService\*.log"
#Write-Host $files
$files |
Select-String -Pattern 'ms 2','ms 4' |
Group-Object Pattern -NoElement
#| Select-Object Count, Name
foreach ($file in $files){
$mss=$file | Select-String -Pattern 'ms 2','ms 4'
foreach ($l in $mss){
$s = $l -split(" ")
$s[9]
$s[9] | Group-Object | Select-Object Count, Name
#Group-Object Pattern -NoElement |
#Select-String -Pattern 'ms 2','ms 4'
}
}
我試圖拆分行,現在我只有我想要的數字。現在我必須數數并制作那張桌子,但我不知道怎么做。我應該使用 Group-object 但它對我不起作用。這是我可以從此代碼獲得的唯一輸出:
Count Name
----- ----
1463 ms 2
1 ms 4
202
Count : 1
Name : 202
202
Count : 1
Name : 202
202
Count : 1
Name : 202
202
Count : 1
Name : 202
uj5u.com熱心網友回復:
Group-Object在這種情況下是正確的解決方案。您只需按與名稱匹配的值進行分組,這樣它就可以計算出使用該模式找到的總數:
Select-String -Path 'C:\Users\krivosik\Desktop\Scripts\logs\PosybeRestEPService\*.log' -Pattern '(?<=\d.*?ms )(2|4|5)\d ' |
Group-Object -Property { $_.Matches.Value } -NoElement
至于模式匹配,使用“正向后視”來確保捕獲name屬性,以防萬一其他東西匹配時出錯ms 2/4/5。
- Postive LookBehind :
(?<=\d.*?ms )確保此模式匹配,然后您才能匹配后面的內容而無需實際捕獲該匹配項。 (2|4|5)\d,這里是name2屬性的實際捕獲,其中包含用于匹配以、4或5僅開頭的模式的選項。
現在,Group-Object可以獲取 的輸出Select-String并按通過 ; 匹配的值對它們進行分組Matches.Value;即2xx, . 4xx_5xx
編輯:找到這些值的路徑已經通過 via 公開,Select-String但您必須使用計算屬性將其顯示出來。此外,如果您想匹配ms 2xx的精確值,則必須將正后視后的正則運算式更改為這些值:
Select-String -Path 'C:\Users\krivosik\Desktop\Scripts\logs\PosybeRestEPService\*.log' -Pattern '(?<=\d.*?ms )(200|202)' |
Group-Object -Property { $_.Matches.Value } |
Select-Object -Property Count, Name, @{
Name = 'Path'
Expression = { $_.Group[0].Path }
}
|用作 "或" RegEx 運算子的定界符,因此它將精確匹配 200或202。
如果你想添加更多的值來精確匹配,只需|在().
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/535819.html
