I have a file containing color codes:
Fri May 25 17:13:04 2012: [....] Starting MTA: exim4^[[?25l^[[?1c^[7^[[1G[^[[32m ok ^[[39;49m^[8^[[?25h^[[?0c.
Fri May 25 17:13:05 2012: [....] Loading cpufreq kernel modules...^[[?25l^[[?1c^[7^[[1G[^[[32m ok ^[[39;49m^[8^[[?25h^[[?0cdone (acpi-cpufreq).
How can I display it colorized on a linux terminal?
Try less -R /your/file
.
I found that more
by default actually does what I'd expect: It shows colorized text in a terminal. The fact that more
worked, while less
(it's younger cousin) didn't made me look at the man less
page.
It turns out that less
supports the -R
flag, which outputs the ESC
sequences as raw control characters. This is the same behavior you get with more
, plus all the search and navigation enhancements that come standard with less
.
For the sake of completeness, the file containing all these escape sequences is generated by the bootlogd daemon (bootlog package in the debian family) which captures all the colorized messages sent to the console during boot. On the console, these messages are first displayed like the following line:
[....] Starting periodic command scheduler: cron
then, when the service or command is executed, an escape sequence is sent to the console to reposition the cursor at the beginning of the line and prints ok, fail, info, warn etc...
[ ok ] Starting periodic command scheduler: cron.
All these messages are captured by the bootlogd daemon and written to a file with all its escape sequences including the repositioning one. No problem except that the ^[
must be replaced by octal 033
to have the file correctly displayed. But, because there is a catch, the daemon also add a date stamp in front of the message without changing the coordinates of the cursor repositioning sequence. Consequently, the ok, fail etc... messages overwrite part of the date stamp. Not nice.
Fri May 25 17:13:01 2012: [....] Starting periodic command scheduler: cron
becomes...
[ ok ay 25 17:13:01 2012: [....] Starting periodic command scheduler: cron.
The solution is to change that cursor positioning sequence. By try and error I found that sequence to be ^[1G
. The following sed command finally get the job done:
sed 's/\^\[/\o33/g;s/\[1G\[/\[27G\[/' /var/log/boot
The bootlogd daemon should purge all the escape sequence before sending the console messages to the file. May we call this a bug?
This "bug" might also be present in all Debian heirs like Ubuntu, Mint etc...
So ripat's answer didn't work for me. I found an alternative on the Debian wiki - https://wiki.debian.org/bootlogd.
At time of writing this is:
sed $'s/\^\[/\E/g' /var/log/boot
For bootlogd version <2.88 (no date stamp). For the later versions:
sed $'s/\^\[/\E/g;s/\[1G\[/\[27G\[/' /var/log/boot
The latter formats all but one line of my log perfectly, with only a small discrepancy. Note also that, as pointed out in the bug report for this issue, the leading $
on the sed pattern makes these solutions bash specific.
You could use the bash built-ins :
$ echo "$(< /your/file)"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With