我有一個包含幾個月日志的檔案,我只想將特定資料的日志存盤到 csv 檔案中。
示例日志資料:
2022-06-21 09:06:09 15SS1B Equip = Z39 Text -0003254 設備已在線添加
2022-07-21 09:06:09 15SS2B Equip = Z40 Text -0003254 設備已添加上線
2022-08-21 09:06:09 15SS3B Equip = Z41 Text -0003254 設備已添加上線
2022-09-21 09:06:09 15SS4B Equip = Z42 Text -0003254 設備已添加上線
我通過以下列獲得結果:IgnoreCase、LineNumber、Line、Filename、Path、Pattern、Context、Matches。我只對 Line 列的結果感興趣。
感謝你的幫助。謝謝。
這是我的代碼:
$data = Get-Content '\log.log' | Select-String -Pattern "2022-08-21" | Export-CSV -Path '\output.csv' -NoTypeInformation
我的要求是獲取該內容并將它們匯出到新表中,例如:
Date Time Number Type
2022-06-21 09:06:09 15SS1B Equip
uj5u.com熱心網友回復:
我會對此使用正則運算式來決議出想要的部分并將它們輸出為物件的屬性:
$dateToSearch = '2022-08-21'
$regex = "^(?<date>$dateToSearch)\s (?<time>\d{2}:\d{2}:\d{2})\s (?<number>[a-z\d] )\s (?<type>\w )\s =\.*"
Get-Content -Path '\log.log' | Where-Object { $_ -match $regex } |
Select-Object @{Name = 'Date'; Expression = {$matches['date']}},
@{Name = 'Time'; Expression = {$matches['time']}},
@{Name = 'Number'; Expression = {$matches['number']}},
@{Name = 'Type'; Expression = {$matches['type']}} |
Export-CSV -Path '\output.csv' -NoTypeInformation
或者使用回圈而不是計算屬性:
$dateToSearch = '2022-08-21'
$regex = "^(?<date>$dateToSearch)\s (?<time>\d{2}:\d{2}:\d{2})\s (?<number>[a-z\d] )\s (?<type>\w )\s =\.*"
Get-Content -Path '\log.log' | Where-Object { $_ -match $regex } | ForEach-Object {
[PsCustomObject]@{
Date = $matches['date']
Time = $matches['time']
Number = $matches['number']
Type = $matches['type']
}
} | Export-CSV -Path '\output.csv' -NoTypeInformation
結果
Date Time Number Type
---- ---- ------ ----
2022-08-21 09:06:09 15SS3B Equip
uj5u.com熱心網友回復:
你在找這樣的東西嗎,我是為一行做的,同樣也可以用于多行。
$line = "2022-06-21 09:06:09 15SS1B Equip = Z39 Text -0003254 Equipment has been added on-line"
$data = $line.Split("=")[0]
$result = $data.Split(" ") # A Space in between quotes
[pscustomObject]@{
Date = $result[0]
Time = $result[1]
Number = $result[2]
Type = $result[3]
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/536289.html
標籤:细绳电源外壳壳数据表出口
