Remove colors codes from a command output

Posted on

Many command line tools on Linux produce colored outputs. This is great when it comes to read it but if you want to pipe it to another tool, the nightmare can easily starts. This tip will show you how to get rid of the colours that can produce some hidden characters that will mess up with your program.

For example, the following command on a typical Linux and Git setup will produce lines of output where each will begin with a colored word:

$ git log --oneline --color
e785d0f Fix submit panel error
d83ff21 Implement remote belt
c0dfae9 Make login fields required
8f69a2a Initial commit

These colored outputs, when read by a human, are convenient, however, they make it difficult to produce complex commands involving pipes and command substitutions. When the following command is executed in a terminal within a Git repository, it will yield the very first commit hash for the repository:

$ git log --oneline --color | tail -n 1 | awk '{print $1}'
8f69a2a

Since the output is colored, the actual content of the output contains some hidden characters. The following command, when executed within a Git repository, will end with a somewhat cryptic error:

$ git show --pretty="format:" --name-only $(git log --oneline --color | tail -n 1 | awk '{print $1}')
bash: $'\E[33ma1d1e20\E[m': command not found
solver/solver.go

Solving this issue is simple, and requires a simple change to the command above. Pipe the colored output through sed, a Unix utility that parses and transforms text, to remove all invisible color codes:

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

You can now try again and observe the result:

$ git show --pretty="format:" --name-only $(git log --oneline --color | \
        tail -n 1 | \
        awk '{print $1}' | \
        sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g")
README.md
app.yaml
index.html
script.js
solver/solver.go
style.css

Source: Toptal engineering blog.