I want to convert a terminal escape-character colored text file
to BBCode
colored text.
For this i've created an Android logfile with logcat -Cd > /sdcard/logcat.txt
. The -C
switch adds the color escape characters. The output looks like this:
[0m[38;5;231mV/Zygote ( 4666): Switching descriptor 55 to /dev/null
[0m[38;5;231mV/Zygote ( 4666): Switching descriptor 9 to /dev/null
[0m[38;5;40mI/ggheart ( 1111): onStop
[0m[38;5;40mI/Test ( 1111): onStop
[0m[38;5;75mD/ActivityThread( 4666): handleBindApplication:com.mxtech.videoplayer.ad
[0m[38;5;75mD/ActivityThread( 4666): setTargetHeapUtilization:0.75
[0m[38;5;75mD/ActivityThread( 4666): setTargetHeapMinFree:2097152
To convert the color codes to BBCode
i've written this sed
script:
#!/bin/bash
#Use 'logcat -Cd > /sdcard/logcat.txt' as input file
sed '/\x1b/ {
s/\x1b\[0m\x1b\[38;5;40m/\[COLOR="Green"\]/
s/\x1b\[0m\x1b\[38;5;196m/\[COLOR="Red"\]/
s/\x1b\[0m\x1b\[38;5;75m/\[COLOR="Blue"\]/
s/\x1b\[0m\x1b\[38;5;166m/\[COLOR="Sienna"\]/
s/\x1b\[0m\x1b\[38;5;231m/\[COLOR="DarkSlateGray"\]/
s/\x1b\[38;5;40m/\[COLOR="Green"\]/
s/\x1b\[38;5;196m/\[COLOR="Red"\]/
s/\x1b\[38;5;75m/\[COLOR="Blue"\]/
s/\x1b\[38;5;166m/\[COLOR="Sienna"\]/
s/\x1b\[38;5;231m/\[COLOR="DarkSlateGray"\]/
s/\x1b\[0m/\[COLOR="Black"\]/
s/$/\[\/COLOR\]/
}' <logcat.txt >logcat2.txt
The processed output text looks like this:
[COLOR="DarkSlateGray"]V/Zygote ( 4666): Switching descriptor 55 to /dev/null[/COLOR]
[COLOR="DarkSlateGray"]V/Zygote ( 4666): Switching descriptor 9 to /dev/null[/COLOR]
[COLOR="Green"]I/ggheart ( 1111): onStop[/COLOR]
[COLOR="Green"]I/Test ( 1111): onStop[/COLOR]
[COLOR="Blue"]D/ActivityThread( 4666): handleBindApplication:com.mxtech.videoplayer.ad[/COLOR]
[COLOR="Blue"]D/ActivityThread( 4666): setTargetHeapUtilization:0.75[/COLOR]
[COLOR="Blue"]D/ActivityThread( 4666): setTargetHeapMinFree:2097152[/COLOR]
This is syntactically correct and works properly in corresponding forum boards but its not optimized and wastes too many characters (that are limited in most boards) due to COLOR
tags not spanning over multiple lines.
It should rather look like this where same colored lines do not close/reopen the same COLOR
tag:
[COLOR="DarkSlateGray"]V/Zygote ( 4666): Switching descriptor 55 to /dev/null
V/Zygote ( 4666): Switching descriptor 9 to /dev/null[/COLOR]
[COLOR="Green"]I/ggheart ( 1111): onStop
I/Test ( 1111): onStop[/COLOR]
[COLOR="Blue"]D/ActivityThread( 4666): handleBindApplication:com.mxtech.videoplayer.ad
D/ActivityThread( 4666): setTargetHeapUtilization:0.75
D/ActivityThread( 4666): setTargetHeapMinFree:2097152[/COLOR]
Any idea how to achieve this? Is it even possible with line based text processor? Its not mandatory as long as it runs on shell/bash/cygwin.
Best Answer
Noting a comment about "Easy using awk", that would be relative to sed.
OP was describing a not-very-well documented feature of
logcat
(see commit) which tellslogcat
to assign hardcoded-colors to each line based on the log entry's priority.There are other choices, such as logcat-color, PID cat and coloredlogcat which let you color different fields, e.g., raising the possibility of multiple colors per line.
Here's an awk script which recognizes the hardcoded logcat colors, but allows multiple colors per line:
With the original example:
you would get the requested output:
but changing the input to
gives this result: