我想打开以下输入:
May 13 00:30:00 BBAOMACBOOKAIR2 syslogd[113]: Configuration Notice:
ASL Module "com.apple.cdscheduler" claims selected messages.
Those messages may not appear in standard system log files or in the ASL database.
May 13 00:30:00 BBAOMACBOOKAIR2 syslogd[113]: Configuration Notice:
ASL Module "com.apple.install" claims selected messages.
Those messages may not appear in standard system log files or in the ASL database.
May 13 00:30:00 BBAOMACBOOKAIR2 syslogd[113]: Configuration Notice:
ASL Module "com.apple.callhistory.asl.conf" claims selected messages.
Those messages may not appear in standard system log files or in the ASL database.
May 13 00:30:00 BBAOMACBOOKAIR2 syslogd[113]: Configuration Notice:
ASL Module "com.apple.authd" sharing output destination "/var/log/asl" with ASL Module "com.apple.asl".
Output parameters from ASL Module "com.apple.asl" override any specified in ASL Module "com.apple.authd".
May 13 00:30:00 BBAOMACBOOKAIR2 syslogd[113]: Configuration Notice:
ASL Module "com.apple.mkb" sharing output destination "/private/var/log/keybagd.log" with ASL Module "com.apple.mkb.internal".
Output parameters from ASL Module "com.apple.mkb.internal" override any specified in ASL Module "com.apple.mkb".
进入以下输出:
May 13 00:30:00 BBAOMACBOOKAIR2 syslogd[113]: Configuration Notice:ASL Module "com.apple.cdscheduler" claims selected messages.Those messages may not appear in standard system log files or in the ASL database.
May 13 00:30:00 BBAOMACBOOKAIR2 syslogd[113]: Configuration Notice:ASL Module "com.apple.install" claims selected messages.Those messages may not appear in standard system log files or in the ASL database.
May 13 00:30:00 BBAOMACBOOKAIR2 syslogd[113]: Configuration Notice:ASL Module "com.apple.callhistory.asl.conf" claims selected messages.Those messages may not appear in standard system log files or in the ASL database.
May 13 00:30:00 BBAOMACBOOKAIR2 syslogd[113]: Configuration Notice:ASL Module "com.apple.authd" sharing output destination "/var/log/asl" with ASL Module "com.apple.asl".Output parameters from ASL Module "com.apple.asl" override any specified in ASL Module "com.apple.authd".
May 13 00:30:00 BBAOMACBOOKAIR2 syslogd[113]: Configuration Notice:ASL Module "com.apple.mkb" sharing output destination "/private/var/log/keybagd.log" with ASL Module "com.apple.mkb.internal".Output parameters from ASL Module "com.apple.mkb.internal" override any specified in ASL Module "com.apple.mkb".
也就是说,缩进的行应该与前面的非缩进的行连接起来。
我首先确保它是一个多行字符串(而不是字符串数组),然后使用 RegEx 根据日期/时间戳进行分割,并为传递的每个多行位修剪行中的任何空格,然后将这些线连接在一起成为一条线。可以用这样的东西来完成
$LogText -join "`n" -split '[\r\n]+\s*(?=\w+ \d+ \d+:\d+:\d+)'|
ForEach-Object {$_.trimstart() -replace '[\r\n]+\s*'}
关于元注释:此答案的否决很可能不是由于答案本身的问题,而可能是某人个人政策的结果,即对他们认为低质量的答案有系统地否决问题。我鼓励每个人亲自尝试此处提供的解决方案,以验证它们是否有效。
假设您的
May ...
行没有前导空格:
Get-Content
-Raw
与基于正则表达式的 -replace
运算符 结合起来(根据需要将输出重定向到文件;如果输入文本是已经在内存中,只需使用 it 作为 LHS):(Get-Content -Raw file.log).TrimEnd() -replace '\r?\n\s+', ' '
switch
和
-File
参数的 -Regex
语句:
& {
$mergedLine = ''
switch -Regex -File file.log {
'^\S' { # 'May ...' line, no leading whitespace.
if ($mergedLine) { $mergedLine } # output previous
$mergedLine = $_
}
default { # Subsequent, indented line (leading whitespace)
$mergedLine += ' ' + $_.TrimStart()
}
}
$mergedLine # output final merged line
}
注:
为了可读性,上面的解决方案放置了一个空格字符。合并(连接)线之间;从上面的代码中删除
' '
的使用,以在没有分隔符的情况下将它们连接起来(如问题中的示例输出所示)。
您可以将
& { ... }
解决方案通过管道传输到 Set-Content
进行输出,但如果性能至关重要,您可能需要使用 System.IO.StreamWriter
.NET 类型来加快写入速度,如这个答案所示.
可以在有关本机 macOS 解决方案的后续问题的
此答案中找到基于
awk
的等效解决方案。