Removing ANSI color codes from Rails logs

Updated . Posted . Visible to the public.

The colors in Rails log files are helpful when watching them but, since they are ANSI color codes like ^[[4;36;1m, can be annoying when you are reading the logs with a tool that does just prints those control characters (like less or vim).

Remove them with sed:

cat staging.log | sed -r "s/\x1B\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g"

This will print the log without colors to your terminal. You can pipe the result into less for example.

To have a file you can vim around with, just write that output into a new file:

cat staging.log | sed -r "s/\x1B\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g" > staging.colorless.log
Arne Hartherz
Last edit
Arne Hartherz
Keywords
linux, unix, terminal, bash, strip, colour
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2011-09-30 14:23)