13

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?

1
  • cat works only after all ^[ are replaced by its octal equivalent 033 see my own answer below. Commented May 26, 2012 at 6:58

4 Answers 4

14

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...

Sign up to request clarification or add additional context in comments.

2 Comments

+1 Same problem here. Your sed formats correctly the log. This is for me a clear bug, and given that in debian the bootlogd package is all about logging: "bootlogd logs all messages printed to the system console during system boot, and records those messages to a logfile." (dpkg -s bootlogd of bootlogd ver. 2.88dsf-22.1), I consider it a critical one.
The output can be piped to less -r. That works well for me, except a single log line from /etc/init.d/kbd in Debian Wheezy.
6

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.

2 Comments

That worked for me also with the exception of one FAIL line, which had a bunch of nonsense text after the FAIL.
This may work on some versions of sed, but \E is not a standard escape sequence. To replace ^[ with an actual escape character, either \o33 or \x1b is more reliable.
5

You could use the bash built-ins :

$ echo "$(< /your/file)"

4 Comments

This worked great for me to get a colorized output from a Jenkins console output.
Worked perfect to get colored output from /var/log/boot.log I just wanted to see if it was doable.. and sure enough it is.
I tried this and it does work. But for larger data files, I wanted to page the output (for navigation and search as well), so I tried piping it through less. Unfortunately, less removed the colorization and replaced the character codes with their human-readable equivalents.
I tried piping it through more, which gave me paging. But I really wanted less's expanded feature set. That led me to discovering my answer.
5

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.