[英]Powershell: Count the occurence of every single “1” string in a txt file
[英]PowerShell Count String in TxT
我有這個腳本。 我可以計算 $err1, $err2, ... 嗎?
$logFolder = "C:\Users\boba\OneDrive\Desktop\TestFolder"
$err1 = "Asus"
$err2 = "Microsoft"
$err3 = "etcetera"
function Read-Date {
param(
[String] $prompt
)
$result = $null
do {
$s = Read-Host $prompt
if ( $s ) {
try {
$result = Get-Date $s
break
}
catch [Management.Automation.PSInvalidCastException] {
Write-Host "Date not valid"
}
}
else {
break
}
}
while ( $true )
$result
}
$startDate = Read-Date "Enter Start Date"
$endDate = Read-Date "Enter End Date"
Get-ChildItem $logFolder -File -Recurse | Select-String -Pattern $err1
Get-ChildItem $logFolder -File -Recurse | Select-String -Pattern $err2
Get-ChildItem $logFolder -File -Recurse | Select-String -Pattern $err3
#...
Where-Object { $_.LastWriteTime -ge $startDate -and $_.LastWriteTime -le $endDate } |
select-Object FullName, LastWriteTime
正是我需要這個腳本來讀取日志文件夾中的兩個日期之間的所有文件中的錯誤。 腳本完成后,創建並打開 .txt(記事本)文件並顯示 $err1 的數量,$err2 的數量...然后在 txt 中逐行顯示錯誤。
有可能的?
謝謝! :)
Machine_Errors.txt - 完成作業后由腳本創建
eg.
***52 errors $err1***
Details:
4 errors in 27.09.2021 - 11:02, 14:22, 19:00, ..
9 errors in 28.09.2021 - 19:12, 20:42, 21:00, ..
***0 errors $err2***
logs.txt(腳本搜索的地方)看起來像這樣
9129318727412asakfwa---------a124314Asus129 27.09.2021 KUR a 11:02
8981284182841ouiruqw---------v9591929912009 27.09.2021 IRA a 11:55
1281284182848oiqwasq---------o901239lMicrosoft91200 28.09.2021 OLI a 20:42
...
你想要的輸出很奇怪。 首先,我建議我們創建一些對象來表示線條。 我在示例日志文件中添加了更多示例。
$tempfile = New-TemporaryFile
@'
9129318727412asakfwa---------a124314Asus129 27.09.2021 KUR a 11:02
8981284182841ouiruqw---------v9591929Microsoft912009 27.09.2021 IRA a 11:55
1281284182848oiqwasq---------o901239lMicrosoft91200 28.09.2021 OLI a 20:42
9129318727412asakfwa---------a124314Asus129 27.09.2021 KUR a 12:22
9129318727412asakfwa---------a124314Asus129 28.09.2021 KUR a 09:11
9129318727412asakfwa---------a124314Microsoft129 28.09.2021 KUR a 04:44
8981284182841ouiruqw---------v959192Microsoft9912009 27.09.2021 IRA a 16:55
1281284182848oiqwasq---------o901239lMicrosoft91200 28.09.2021 OLI a 17:42
'@ | Set-Content $tempfile -Encoding UTF8
使用您的Read-Date
函數,我適當地設置了$startDate
和$endDate
。 然后使用 switch 語句讀取文件,同時提取 3 位信息。 最后輸出的是一個具有 3 個屬性的 PSCustomObject,供應商、日期、時間,其中日期在提供的日期內。
$startDate = Read-Date "Enter Start Date"
$endDate = Read-Date "Enter End Date"
$output = switch -Regex -File $tempfile {
'(\S+)\s(\d{2}\.\d{2}\.\d{4}).+(\d{2}:\d{2})$' {
$date,$time = $matches[2..3]
$compare = [datetime]::ParseExact($date,'dd.MM.yyyy',$null)
if($compare -le $startDate -and $compare -ge $endDate){
continue
}
$vendor = switch -Regex ($matches.1){
'asus' {'ASUS'}
'microsoft' {'Microsoft'}
'etcetera' {'etcetera'}
default {'Unknown'}
}
[PSCustomObject]@{
Vendor = $vendor
Date = $date
Time = $time
}
}
}
現在我們可以使用我們的好對象並進行一些分組以提取所需的信息並以指定的格式輸出。
$output | Group-Object -Property Vendor | Sort-Object -Property count -Descending | ForEach-Object {
"***$($_.count) errors $($_.name)***"
$_.group | Group-Object -Property Date | Sort-Object -Property count -Descending | ForEach-Object -Begin {" Details:"} {
" $($_.count) errors in $($_.group.date|Select-Object -First 1) - $($_.group.time -join ', ')"
}
""
}
這是輸出
***5 errors Microsoft***
Details:
3 errors in 28.09.2021 - 20:42, 04:44, 17:42
2 errors in 27.09.2021 - 11:55, 16:55
***3 errors ASUS***
Details:
2 errors in 27.09.2021 - 11:02, 12:22
1 errors in 28.09.2021 - 09:11
現在您可以只使用Set-Content
寫入文本文件。
管道到Set-Content
$output | Group-Object -Property Vendor | Sort-Object -Property count -Descending | ForEach-Object {
"***$($_.count) errors $($_.name)***"
$_.group | Group-Object -Property Date | Sort-Object -Property count -Descending | ForEach-Object -Begin {" Details:"} {
" $($_.count) errors in $($_.group.date|Select-Object -First 1) - $($_.group.time -join ', ')"
}
""
} | Set-Content -Path \path\to\textfile.txt -Encoding UTF8
或與子表達式內聯
Set-Content -Path \path\to\textfile.txt -Encoding UTF8 -Value ($output | Group-Object -Property Vendor | Sort-Object -Property count -Descending | ForEach-Object {
"***$($_.count) errors $($_.name)***"
$_.group | Group-Object -Property Date | Sort-Object -Property count -Descending | ForEach-Object -Begin {" Details:"} {
" $($_.count) errors in $($_.group.date|Select-Object -First 1) - $($_.group.time -join ', ')"
}
""
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.