Image of XSLT 2.0 and XPath 2.0 Programmer's Reference (Programmer to Programmer)
Image of Modern Operating Systems (3rd Edition)
Image of Advanced Programming in the UNIX Environment, Second Edition (Addison-Wesley Professional Computing Series)
Image of RHCE Red Hat Certified Engineer Linux Study Guide (Exam RH302) (Certification Press)

Remove Color Code Escape Sequences

Here is a simple way using sed to remove special characters such as color codes and other escape sequences from each line in a log file or the output from a utility like screen.

$ sed -r "s/x1B[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"

If you wish to see the actual escape sequences, one way is to use the -r or -R options to the less utility

-r or --raw-control-chars
    Causes "raw" control characters to be displayed. The default is to display control characters using the caret notation; for example, a control-A (octal 001) is displayed as "^A". Warning: when the -r option is used, less cannot keep track of the actual appearance of the screen (since this depends on how the screen responds to each type of control character). Thus, various display problems may result, such as long lines being split in the wrong place.
    Like -r, but only ANSI "color" escape sequences are output in "raw" form. Unlike -r, the screen appearance is maintained correctly in most cases.

cat -vte will also display all escape sequences.

Comments are closed.