如何在PowerShell中识别和拦截后连接字符



我想转换以下输入:

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 '[rn]+s*(?=w+ d+ d+:d+:d+)'|
ForEach-Object {$_.trimstart() -replace '[rn]+s*'}

假设您的May ...行没有前导空格:

  • 如果文件足够小,可以将整个文件放入内存中,则将Get-Content-Raw与基于正则表达式的-replace操作符组合在一起(根据需要将输出重定向到文件;如果输入文本已经在内存中,只需使用作为LHS):
(Get-Content -Raw file.log).TrimEnd() -replace 'r?ns+', ' '
  • 否则,使用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类型来加快编写,如本答案所示。

一个等效的基于awk的解决方案可以在这个答案中找到关于本机macOS解决方案的后续问题。

最新更新