我有一个文件夹,里面有几个 .log 文件,有两种类型。每个日志文件的末尾都有一行包含以下信息:
I: -11.4
,在“I:”之前有 2 个空格,之后有 10 个空格。我试图生成的是一个 .txt 文件,该文件仅以低于 -11.9 的值命名,例如:
值日志.txt
namefile.log -12.0
namefile.log -12.5
namefile.log -13.0
namefile.log -14.0
我正在尝试使用在链接中找到的命令:
docs.microsoft\select-字符串
superuser.com/questions/1589183/powershell-command-to-find-string-in-a-text-file
命令:
Get-ChildItem "C:\Users\username\Desktop\Logs\*.log" | Where-Object { $_.Attributes -ne "Directory" } | ForEach-Object {
If (Get-Content $_.FullName | Select-String -Pattern "I:") {
New-Object -TypeName PSCustomObject -Property @{
Value = $A} | Out-File "C:\Users\username\Desktop\valuelogs.log" -Append
}
}
但它没有生成包含我所需信息的输出文件。
已编辑:
以下是文件的内容:
注意:预期值是最后一个值,位于“综合响度:”行下方,它是每行中存在的所有 I:值的平均值。
文件内容:
[Parsed_ebur128_0 @ 000001e14c3b09c0] t: 198.4 TARGET:-23 LUFS M: -49.8 S: -26.7 I: -11.4 LUFS LRA: 2.4 LU
[Parsed_ebur128_0 @ 000001e14c3b09c0] t: 198.5 TARGET:-23 LUFS M: -50.9 S: -27.1 I: -11.4 LUFS LRA: 2.4 LU
[Parsed_ebur128_0 @ 000001e14c3b09c0] t: 198.6 TARGET:-23 LUFS M: -51.6 S: -27.3 I: -11.4 LUFS LRA: 2.4 LU
[Parsed_ebur128_0 @ 000001e14c3b09c0] t: 198.7 TARGET:-23 LUFS M: -53.0 S: -27.4 I: -11.4 LUFS LRA: 2.4 LU
[Parsed_ebur128_0 @ 000001e14c3b09c0] t: 198.8 TARGET:-23 LUFS M: -53.6 S: -27.5 I: -11.4 LUFS LRA: 2.4 LU
[Parsed_ebur128_0 @ 000001e14c3b09c0] t: 198.9 TARGET:-23 LUFS M: -58.6 S: -28.3 I: -11.4 LUFS LRA: 2.4 LU
[Parsed_ebur128_0 @ 000001e14c3b09c0] t: 199 TARGET:-23 LUFS M: -61.8 S: -28.9 I: -11.4 LUFS LRA: 2.4 LU
[Parsed_ebur128_0 @ 000001e14c3b09c0] t: 199.1 TARGET:-23 LUFS M: -65.0 S: -31.2 I: -11.4 LUFS LRA: 2.4 LU
[Parsed_ebur128_0 @ 000001e14c3b09c0] t: 199.2 TARGET:-23 LUFS M: -68.1 S: -31.9 I: -11.4 LUFS LRA: 2.4 LU
Integrated loudness:
I: -11.4 LUFS
Threshold: -21.5 LUFS
第二个日志文件内容:
请注意,在此日志中我想要的值位于Input Integrated:
size=N/A time=00:02:36.90 bitrate=N/A speed=26.1x
size=N/A time=00:02:50.00 bitrate=N/A speed=26.1x
size=N/A time=00:03:03.20 bitrate=N/A speed=26.1x
size=N/A time=00:03:16.50 bitrate=N/A speed=26.1x
size=N/A time=00:03:29.90 bitrate=N/A speed=26.2x
size=N/A time=00:03:33.26 bitrate=N/A speed=26.3x
[Parsed_loudnorm_0 @ 00000193badb3000]
Input Integrated: -7.2 LUFS
Input True Peak: +1.5 dBTP
Input LRA: 3.5 LU
Input Threshold: -17.4 LUFS
答案1
尝试这个
$logMatches = Select-String -Path "C:\Users\username\Desktop\Logs\*.log" -Pattern '(?<I>^ +I:) +(?<Number>.+)|(?<I>^Input Integrated:) +(?<Number>.+)' -List | Select-Object -Property FileName -ExpandProperty Matches
$results = foreach ($log in $logMatches) {
$number = $log.Groups | Where-Object { $_.Name -eq "Number" }
[PSCustomObject]@{
FileName = $log.Filename
Number = [decimal]$($number.Value -replace " .*")
}
}
$results | Where-Object { $_.Number -lt -11.9 } | Out-File "C:\Users\username\Desktop\valuelogs.log"