Terminal Colors – How to Clean stdout/stderr Color Output

colorsterminal

I have a program that would output something like this:

^[0;33m"2015-02-09 11:42:36 +0700 114.125.x.x access"^[0m

Is there built in linux program that could clean that output up into something like this

"2015-02-09 11:42:36 +0700 114.125.x.x access"

Best Answer

Those are ANSI control sequences. There are no programs built-in that remove those codes, at least that I am aware of. A simple sed script, however, will the job for you:

sed -r 's/\x1b_[^\x1b]*\x1b[\]//g; s/\x1B\[[^m]*m//g'

Using the above with your sample input:

$ echo $'\e[0;33m"2015-02-09 11:42:36 +0700 114.125.x.x access"\e[0m'  | sed -r 's/\x1b_[^\x1b]*\x1b[\]//g; s/\x1B\[[^m]*m//g'                    
"2015-02-09 11:42:36 +0700 114.125.x.x access"

OSX or other BSD system

With OSX (BSD) sed, commands cannot be chained together with semicolons. Try, instead:

sed -e 's/\x1b_[^\x1b]*\x1b[\]//g' -e 's/\x1B\[[^m]*m//g'
Related Question