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:2097152To convert the color codes to BBCode i've written this sed script:#!/bin/bash#Use 'logcat -Cd > /sdcard/logcat.txt' as input filesed '/\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.txtThe 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/nullV/Zygote  ( 4666): Switching descriptor 9 to /dev/null[/COLOR][COLOR=Green]I/ggheart ( 1111): onStopI/Test    ( 1111): onStop[/COLOR][COLOR=Blue]D/ActivityThread( 4666): handleBindApplication:com.mxtech.videoplayer.adD/ActivityThread( 4666): setTargetHeapUtilization:0.75D/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.
Bash - Convert escape character colored text to BBCode
shell script;text processing;sed;escape characters;replace