PowerShell识别截取后如何拼接字符

How to concatenate characters after recognition and interception in 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 根据 date/time 标记进行拆分,并且对于每个传递的多行位 trim 一行中的任何空格,并将这些行连接成一行。这可以用这样的东西来完成

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

假设您的 May ... 行没有前导白色space:

  • 如果文件足够小,可以作为一个整体放入内存,合并Get-Content -Raw with the regex-based (根据需要将输出重定向到一个文件;如果输入文本已经在内存中,只需使用作为左轴):
(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
}

注:

  • 为了便于阅读,上面的解决方案放置了一个 space 字符。在合并(连接)线之间;从上面的代码中删除 ' ' 的使用以在没有分隔符的情况下加入它们(如您问题的示例输出中所示)。

  • 您可以将 & { ... } 解决方案传送到 Set-Content for output, though if performance is paramount, you may want to use the System.IO.StreamWriter .NET type for faster writing, as shown in

一个等效的基于 awk 的解决方案可以在 this answer 中找到你关于原生 macOS 解决方案的后续问题。