I want to turn the following input:
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.
into the following output:
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.
That is, the indented lines should be joined to the preceding non-indented line.
I already have a PowerShell solution, but now I need a solution using native macOS utilities, such as a bash
solution.
Here is the PowerShell solution, from this answer to my previous question:
$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
Here is my attempt at converting this to a bash
script:
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
Unfortunately, it isn't working as intended, because I receive the following output:
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.