【发布时间】:2021-11-05 11:49:54
【问题描述】:
我想转以下输入:
May 13 00:29:49 BBAOMACBOOKAIR2 com.apple.xpc.launchd[1] (com.apple.mdworker.bundles[12610]): Service exited with abnormal code: 78
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:29:49 BBAOMACBOOKAIR2 com.apple.xpc.launchd[1] (com.apple.mdworker.bundles[12610]): Service exited with abnormal code: 78
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.
也就是说,缩进的行应该连接到前面的非缩进行。
我已经有一个 PowerShell 解决方案,但现在我需要一个使用 native macOS 实用程序的解决方案,例如 bash 解决方案。
这里是 PowerShell 解决方案,从 this answer 到我之前的问题:
$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
这是我将其转换为bash 脚本的尝试:
file=/xx
OIFS=$IFS
IFS=
while read -r line
do
case $line in
[a-zA-Z]*)
if [ $line ];then
line=$line
fi
y=$line
;;
*)
line=$y$line
;;
esac
echo $line
done <$file
IFS=$OIFS
不幸的是,它没有按预期工作,因为我收到以下输出:
May 13 00:29:49 BBAOMACBOOKAIR2 com.apple.xpc.launchd[1] (com.apple.mdworker.bundles[12610]): Service exited with abnormal code: 78
May 13 00:30:00 BBAOMACBOOKAIR2 syslogd[113]: Configuration Notice:
May 13 00:30:00 BBAOMACBOOKAIR2 syslogd[113]: Configuration Notice: ASL Module "com.apple.cdscheduler" claims selected messages.
May 13 00:30:00 BBAOMACBOOKAIR2 syslogd[113]: Configuration Notice: 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:
May 13 00:30:00 BBAOMACBOOKAIR2 syslogd[113]: Configuration Notice: ASL Module "com.apple.install" claims selected messages.
May 13 00:30:00 BBAOMACBOOKAIR2 syslogd[113]: Configuration Notice: Those messages may not appear in standard system log files or in the ASL database.
【问题讨论】:
-
很好听。仅供参考:PowerShell(核心)也runs on a mac
-
@iRon 我知道可以。但我仍然需要 bash/shell。如果可以,请撤消您对这个问题的投票。谢谢。
-
对 PowerShell 版本的评论:您使用 .Net 类
System.IO.StreamReader(而不是本机Get-Contentcmdlet)可能用于 performance reasons,但类似于 building object collections,字符串是不可变的,并且因此,增加赋值运算符 (+=) 可能会变得相当昂贵。相反,您最好将每一行放在管道上,然后-Join他们一次。 -
@iRon 是的,因为性能。文件太大。谢谢你的建议。我也将学习如何提出一个好问题。
标签: bash macos powershell shell