How to remove the decorate colors characters in bash output?
Asked Answered
R

2

5

A console program (translate-shell) has an output with colors and uses special decorate characters for this: ^[[22m, ^[[24m, ^[[1m... and so on.

I'd like to remove them to get a plain text.

I tried with tr -d "^[[22m" and with sed 's/[\^[[22m]//g', but only is removed the number, not the special character ^[

Thanks.

Rajput answered 23/8, 2015 at 13:8 Comment(0)
D
10

You have multiple options:

EDIT

The solution from commandlinefu does the job pretty well:

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

The solution from unix.stackexchange might be better but is much longer and so you would want to create a separate script file because it is so long instead of just doing a shell one-liner.

Dorene answered 23/8, 2015 at 13:38 Comment(3)
The option in commandlinefu works perfectly, thanks: sed -r "s/\x1B[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"Rajput
This is a link-only answer; please provide the relevant details in the answer itself.Ge
the 's/\x1b\[[0-9;]*m//g' regex works betterLatea
M
2

I found this in the manual about the use of ANSI escape codes:

-no-ansi
    Do not use ANSI escape codes.

So you should add this option when starting the program.

Misdirect answered 23/8, 2015 at 13:19 Comment(2)
No, that option remove other characters too (á é í ó ú ñ...)Rajput
@GeaPlanet Then you are doing something wrong. Those characters are NOT ansi escapes and should not be effected by -no-ansi. This sounds more like a configuration problem to me (or a bug in translate-shell).Misdirect

© 2022 - 2024 — McMap. All rights reserved.