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

bfrts1fy  于 2023-10-18  发布在  Shell
关注(0)|答案(2)|浏览(130)

我想把下面的输入:

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".

也就是说,缩进的行应该连接到前面的非缩进行。

djmepvbi

djmepvbi1#

我首先会确保它是一个多行字符串(而不是一个字符串数组),然后使用RegEx根据日期/时间戳进行拆分,并为传递的每个多行位修剪行中的任何空格,并将行连接到一行中。可以用这样的东西来完成

$LogText -join "`n" -split '[\r\n]+\s*(?=\w+ \d+ \d+:\d+:\d+)'|
    ForEach-Object {$_.trimstart() -replace '[\r\n]+\s*'}
e3bfsja2

e3bfsja22#

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

  • 如果文件足够小,可以放入整个内存中,则将合并Get-Content-Raw与基于正则表达式的-replace operator结合(根据需要将输出重定向到文件;如果输入文本已经在内存中,只需使用 it 作为LHS):
(Get-Content -Raw file.log).TrimEnd() -replace '\r?\n\s+', ' '
& {
  $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类型以实现更快的写入,如this answer中所示。

this answer中可以找到一个基于awk的等效解决方案,以解决您关于本机macOS解决方案的后续问题。

相关问题