Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter special chars such as color codes from shell output

Tags:

python

shell

I ran shell commands in python, logged their outputs into files and finally showed on a web page. however the color style chars of command outputs were also logged. is there a way to filter out the color style chars or display them correctly on web pages? Many thanks!

Output log:

" 22200K .......\u001b[0m\u001b[91m... .......... ...\u001b[0m\u001b[91m.\u001b[0m\u001b[91m...... .........\u001b[0m\u001b[91m.\u001b[0m\u001b[91m \u001b[0m\u001b[91m.\u001b[0m\u001b[91m.\u001b[0m\u001b[91m.\u001b[0m\u001b[91m.\u001b[0m\u001b[91m...... 50% 28.6K 12m55s"

the real text:

[INFO][88] 22250K .......... .......... .......... .......... .......... 50% 35.8K 12m53s
like image 514
ucdream Avatar asked May 24 '15 15:05

ucdream


People also ask

How many ANSI colors are there?

ANSI and Safety Colors. ANSI has outlined 10 safety colors for visual communication—and specific applications for each—in the ANSI Z535 safety sign standard.

How do ANSI escape codes work?

ANSI escape sequences are a standard for in-band signaling to control cursor location, color, font styling, and other options on video text terminals and terminal emulators. Certain sequences of bytes, most starting with an ASCII escape character and a bracket character, are embedded into text.

How do I delete ANSI characters?

You can use regexes to remove the ANSI escape sequences from a string in Python. Simply substitute the escape sequences with an empty string using re. sub(). The regex you can use for removing ANSI escape sequences is: '(\x9B|\x1B\[)[0-?]


1 Answers

In the unlikely case when you have xterm256 color codes as well, this will filter both 'normal' ansi and xterm256 ansi codes:

import re
print re.sub(r'\x1b(\[.*?[@-~]|\].*?(\x07|\x1b\\))', '', a)

or in a slightly less obfuscated and more readable form:

'(' + CSI + '.*?' + CMD + '|' + OSC + '.*?' + '(' + ST + '|' + BEL + ')' + ')'

Complete code with tests:

import re

tests = [
    u"22200K .......\u001b[0m\u001b[91m... .......... ...\u001b[0m\u001b[91m.\u001b[0m\u001b[91m...... .........\u001b[0m\u001b[91m.\u001b[0m\u001b[91m \u001b[0m\u001b[91m.\u001b[0m\u001b[91m.\u001b[0m\u001b[91m.\u001b[0m\u001b[91m.\u001b[0m\u001b[91m...... 50% 28.6K 12m55s",
    u"=\u001b[m=",
    u"-\u001b]23\u0007-",
]

def trim_ansi(a):
    ESC = r'\x1b'
    CSI = ESC + r'\['
    OSC = ESC + r'\]'
    CMD = '[@-~]'
    ST = ESC + r'\\'
    BEL = r'\x07'
    pattern = '(' + CSI + '.*?' + CMD + '|' + OSC + '.*?' + '(' + ST + '|' + BEL + ')' + ')'
    return re.sub(pattern, '', a)

for t in tests:
    print trim_ansi(t)
like image 185
jettico Avatar answered Sep 30 '22 01:09

jettico