Remove color codes (special characters) with sed

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

By: Cowboy
2009-09-23 12:06:33

10 Alternatives + Submit Alt

What Others Think

See also for other possible codes to strip
pixelbeat · 573 weeks and 4 days ago
Just what I needed for cleaning the capture of the 'script' command. Thanks!
funollet · 567 weeks and 6 days ago
Does this work when you have 2 colors on a line?
matthewbauer · 567 weeks and 2 days ago
badass. nice seddage :) i wonder if there is a simpler way to do it with simple redirection of stdout.. hmm not sure yet. anyway awesome, i'm using this for now xD
kevinquinnyo · 450 weeks and 1 day ago
FWIW, the `-r` does not work with `sed` on Mac OS-X.
jaytaylor · 377 weeks and 3 days ago
Is there any way to undo the effect of sed command?
Wajiha · 335 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: