A lot of output can be generated when you compiling large projects. When it breaks, it can be difficult to identify the particular spot in the build-process where things when wrong. Highlighting the error messages can help them stand out from the rest of the output.
ANSI escape sequences can be used to modify how your terminal window displays its text. For example, outputting the sequence
You can highlight certain messages by routing the STDOUT and STDERR streams to sed and performing a replacement.
The tricky part is quoting and escaping the expression correctly so various meta-characters aren't intercepted by the shell. And some implementations of sed won't correctly convert
ANSI escape sequences can be used to modify how your terminal window displays its text. For example, outputting the sequence
\033[41;37mHello World\033[0m
would result in "Hello World" displayed in white text against a red background. Escape sequences begin with an escape character (ASCII 27, octal 033) and bracket. The control values are then given (multiple values are semicolon-separated) and the entire sequence closes with m
.You can highlight certain messages by routing the STDOUT and STDERR streams to sed and performing a replacement.
s,(.*error.*|.*fail.*|.*undef.*),\033[41;37m\1\033[0m,giThe values you match are of course entirely up to your discretion.
The tricky part is quoting and escaping the expression correctly so various meta-characters aren't intercepted by the shell. And some implementations of sed won't correctly convert
\033
to an escape character, so you may need to enter it directly by typing CTRL+V
, CTRL+[
. Depending on your terminal, an actual escape character may be displayed as ^[
or a special glyph like when you enter it.make install 2>&1 | sed -e \ 's,\(.*error.*\|.*fail.*\|.*undef.*\),[41;37m\1[0m,gi'If you find yourself using such highlighting often, you may want to define a function to save yourself some typing. For example, with bash you can add something like this to your .bashrc file:
function make() { /usr/bin/make $@ 2>&1 | sed -e \ 's,\(.*error.*\|.*fail.*\|.*undef.*\),[41;37m\1[0m,gi'; }You can type
make install
at the prompt like you normally would. bash will call the new make()
function, which in turn calls the actual make utility with any arguments (such as install
) and colorizes the output. Error highlighting is now seamless and automatic!
Why haven't I ever thought of that? Good post Tim!
ReplyDelete