From Newsgroup: comp.unix.shell
ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:
Usage:
ansi-strip [OPTIONS] [FILE]
. . .
python3 ansi-strip.py file.txt > clean.txt
cat file.txt | ansi-strip
If you don't set up the script the right way, you can just run
it with "python3 ansi-strip.py". But you can also set it up
so you just call it with "ansi-strip" instead, like by adding
a shebang, using chmod, and renaming it.
Here's another demo:
Output:
reset: output: '][', input: ']\x1b[0m['
bold: output: '][', input: ']\x1b[1m['
dim: output: '][', input: ']\x1b[2m['
italic: output: '][', input: ']\x1b[3m['
underline: output: '][', input: ']\x1b[4m['
blink: output: '][', input: ']\x1b[5m['
reverse: output: '][', input: ']\x1b[7m['
hidden: output: '][', input: ']\x1b[8m['
strikethrough: output: '][', input: ']\x1b[9m['
fg_black: output: '][', input: ']\x1b[30m['
fg_red: output: '][', input: ']\x1b[31m['
fg_green: output: '][', input: ']\x1b[32m['
fg_yellow: output: '][', input: ']\x1b[33m['
fg_blue: output: '][', input: ']\x1b[34m['
fg_magenta: output: '][', input: ']\x1b[35m['
fg_cyan: output: '][', input: ']\x1b[36m['
fg_white: output: '][', input: ']\x1b[37m['
fg_default: output: '][', input: ']\x1b[39m['
bg_black: output: '][', input: ']\x1b[40m['
bg_red: output: '][', input: ']\x1b[41m['
bg_green: output: '][', input: ']\x1b[42m['
bg_yellow: output: '][', input: ']\x1b[43m['
bg_blue: output: '][', input: ']\x1b[44m['
bg_magenta: output: '][', input: ']\x1b[45m['
bg_cyan: output: '][', input: ']\x1b[46m['
bg_white: output: '][', input: ']\x1b[47m['
bg_default: output: '][', input: ']\x1b[49m['
fg_bright_black: output: '][', input: ']\x1b[90m['
fg_bright_red: output: '][', input: ']\x1b[91m['
fg_bright_green: output: '][', input: ']\x1b[92m['
fg_bright_yellow: output: '][', input: ']\x1b[93m['
fg_bright_blue: output: '][', input: ']\x1b[94m['
fg_bright_magenta: output: '][', input: ']\x1b[95m['
fg_bright_cyan: output: '][', input: ']\x1b[96m['
fg_bright_white: output: '][', input: ']\x1b[97m['
bg_bright_black: output: '][', input: ']\x1b[100m['
bg_bright_red: output: '][', input: ']\x1b[101m['
bg_bright_green: output: '][', input: ']\x1b[102m['
bg_bright_yellow: output: '][', input: ']\x1b[103m['
bg_bright_blue: output: '][', input: ']\x1b[104m['
bg_bright_magenta: output: '][', input: ']\x1b[105m['
bg_bright_cyan: output: '][', input: ']\x1b[106m['
bg_bright_white: output: '][', input: ']\x1b[107m['
cursor_up: output: '][', input: ']\x1b[1A['
cursor_down: output: '][', input: ']\x1b[1B['
cursor_forward: output: '][', input: ']\x1b[1C['
cursor_back: output: '][', input: ']\x1b[1D['
cursor_home: output: '][', input: ']\x1b[H['
fg_256_1: output: '][', input: ']\x1b[38;5;34m['
fg_256_2: output: '][', input: ']\x1b[38;5;198m['
fg_256_3: output: '][', input: ']\x1b[38;5;117m['
fg_256_4: output: '][', input: ']\x1b[38;5;202m['
fg_256_5: output: '][', input: ']\x1b[38;5;9m['
fg_256_6: output: '][', input: ']\x1b[38;5;226m['
fg_256_7: output: '][', input: ']\x1b[38;5;45m['
fg_256_8: output: '][', input: ']\x1b[38;5;160m['
fg_256_9: output: '][', input: ']\x1b[38;5;240m['
fg_256_10: output: '][', input: ']\x1b[38;5;75m['
bg_256_1: output: '][', input: ']\x1b[48;5;33m['
bg_256_2: output: '][', input: ']\x1b[48;5;129m['
bg_256_3: output: '][', input: ']\x1b[48;5;221m['
bg_256_4: output: '][', input: ']\x1b[48;5;196m['
bg_256_5: output: '][', input: ']\x1b[48;5;15m['
bg_256_6: output: '][', input: ']\x1b[48;5;100m['
bg_256_7: output: '][', input: ']\x1b[48;5;5m['
bg_256_8: output: '][', input: ']\x1b[48;5;240m['
bg_256_9: output: '][', input: ']\x1b[48;5;28m['
bg_256_10: output: '][', input: ']\x1b[48;5;93m['
fg_rgb_1: output: '][', input: ']\x1b[38;2;12;200;150m[' fg_rgb_2: output: '][', input: ']\x1b[38;2;255;180;60m[' fg_rgb_3: output: '][', input: ']\x1b[38;2;90;20;240m[' fg_rgb_4: output: '][', input: ']\x1b[38;2;10;175;75m[' fg_rgb_5: output: '][', input: ']\x1b[38;2;200;40;180m[' fg_rgb_6: output: '][', input: ']\x1b[38;2;70;255;10m[' fg_rgb_7: output: '][', input: ']\x1b[38;2;250;120;120m[' fg_rgb_8: output: '][', input: ']\x1b[38;2;30;200;255m[' fg_rgb_9: output: '][', input: ']\x1b[38;2;220;220;90m[' fg_rgb_10: output: '][', input: ']\x1b[38;2;180;80;20m[' bg_rgb_1: output: '][', input: ']\x1b[48;2;220;40;70m[' bg_rgb_2: output: '][', input: ']\x1b[48;2;15;120;255m[' bg_rgb_3: output: '][', input: ']\x1b[48;2;80;200;120m[' bg_rgb_4: output: '][', input: ']\x1b[48;2;160;90;240m[' bg_rgb_5: output: '][', input: ']\x1b[48;2;255;220;180m[' bg_rgb_6: output: '][', input: ']\x1b[48;2;100;40;20m[' bg_rgb_7: output: '][', input: ']\x1b[48;2;250;150;10m[' bg_rgb_8: output: '][', input: ']\x1b[48;2;30;255;170m[' bg_rgb_9: output: '][', input: ']\x1b[48;2;170;170;210m[' bg_rgb_10: output: '][', input: ']\x1b[48;2;200;200;30m[' cursor_pos_1: output: '][', input: ']\x1b[3;5H['
cursor_pos_2: output: '][', input: ']\x1b[10;15H['
cursor_pos_3: output: '][', input: ']\x1b[40;20H['
cursor_pos_4: output: '][', input: ']\x1b[25;60H['
cursor_pos_5: output: '][', input: ']\x1b[5;30H['
cursor_pos_6: output: '][', input: ']\x1b[12;12H['
cursor_pos_7: output: '][', input: ']\x1b[70;10H['
cursor_pos_8: output: '][', input: ']\x1b[15;45H['
cursor_pos_9: output: '][', input: ']\x1b[1;80H['
cursor_pos_10: output: '][', input: ']\x1b[50;25H['
erase_line: output: '][', input: ']\x1b[2K['
erase_to_end: output: '][', input: ']\x1b[K['
erase_screen: output: '][', input: ']\x1b[2J[' erase_screen_from_cursor: output: '][', input: ']\x1b[J['
save_cursor: output: '][', input: ']\x1b[s['
restore_cursor: output: '][', input: ']\x1b[u['
alt_screen_on: output: '][', input: ']\x1b[?1049h['
alt_screen_off: output: '][', input: ']\x1b[?1049l['
Source code (to be called in the context of the
previous script):
def demo1():
# ANSI Escape Sequences Demonstration in Python
# Reset
reset = "\033[0m"
# Text style
bold = "\033[1m"
dim = "\033[2m"
italic = "\033[3m"
underline = "\033[4m"
blink = "\033[5m"
reverse = "\033[7m"
hidden = "\033[8m"
strikethrough = "\033[9m"
# Foreground colors (basic)
fg_black = "\033[30m"
fg_red = "\033[31m"
fg_green = "\033[32m"
fg_yellow = "\033[33m"
fg_blue = "\033[34m"
fg_magenta = "\033[35m"
fg_cyan = "\033[36m"
fg_white = "\033[37m"
fg_default = "\033[39m"
# Background colors (basic)
bg_black = "\033[40m"
bg_red = "\033[41m"
bg_green = "\033[42m"
bg_yellow = "\033[43m"
bg_blue = "\033[44m"
bg_magenta = "\033[45m"
bg_cyan = "\033[46m"
bg_white = "\033[47m"
bg_default = "\033[49m"
# Bright foreground colors
fg_bright_black = "\033[90m"
fg_bright_red = "\033[91m"
fg_bright_green = "\033[92m"
fg_bright_yellow = "\033[93m"
fg_bright_blue = "\033[94m"
fg_bright_magenta = "\033[95m"
fg_bright_cyan = "\033[96m"
fg_bright_white = "\033[97m"
# Bright backgrounds
bg_bright_black = "\033[100m"
bg_bright_red = "\033[101m"
bg_bright_green = "\033[102m"
bg_bright_yellow = "\033[103m"
bg_bright_blue = "\033[104m"
bg_bright_magenta = "\033[105m"
bg_bright_cyan = "\033[106m"
bg_bright_white = "\033[107m"
# Cursor movement
cursor_up = "\033[1A"
cursor_down = "\033[1B"
cursor_forward = "\033[1C"
cursor_back = "\033[1D"
cursor_home = "\033[H"
# 256-color (foreground and background)
fg_256_1 = "\033[38;5;34m"
fg_256_2 = "\033[38;5;198m"
fg_256_3 = "\033[38;5;117m"
fg_256_4 = "\033[38;5;202m"
fg_256_5 = "\033[38;5;9m"
fg_256_6 = "\033[38;5;226m"
fg_256_7 = "\033[38;5;45m"
fg_256_8 = "\033[38;5;160m"
fg_256_9 = "\033[38;5;240m"
fg_256_10 = "\033[38;5;75m"
bg_256_1 = "\033[48;5;33m"
bg_256_2 = "\033[48;5;129m"
bg_256_3 = "\033[48;5;221m"
bg_256_4 = "\033[48;5;196m"
bg_256_5 = "\033[48;5;15m"
bg_256_6 = "\033[48;5;100m"
bg_256_7 = "\033[48;5;5m"
bg_256_8 = "\033[48;5;240m"
bg_256_9 = "\033[48;5;28m"
bg_256_10 = "\033[48;5;93m"
# True color (RGB foreground and background)
fg_rgb_1 = "\033[38;2;12;200;150m"
fg_rgb_2 = "\033[38;2;255;180;60m"
fg_rgb_3 = "\033[38;2;90;20;240m"
fg_rgb_4 = "\033[38;2;10;175;75m"
fg_rgb_5 = "\033[38;2;200;40;180m"
fg_rgb_6 = "\033[38;2;70;255;10m"
fg_rgb_7 = "\033[38;2;250;120;120m"
fg_rgb_8 = "\033[38;2;30;200;255m"
fg_rgb_9 = "\033[38;2;220;220;90m"
fg_rgb_10 = "\033[38;2;180;80;20m"
bg_rgb_1 = "\033[48;2;220;40;70m"
bg_rgb_2 = "\033[48;2;15;120;255m"
bg_rgb_3 = "\033[48;2;80;200;120m"
bg_rgb_4 = "\033[48;2;160;90;240m"
bg_rgb_5 = "\033[48;2;255;220;180m"
bg_rgb_6 = "\033[48;2;100;40;20m"
bg_rgb_7 = "\033[48;2;250;150;10m"
bg_rgb_8 = "\033[48;2;30;255;170m"
bg_rgb_9 = "\033[48;2;170;170;210m"
bg_rgb_10 = "\033[48;2;200;200;30m"
# Cursor position samples (x, y values)
cursor_pos_1 = "\033[3;5H"
cursor_pos_2 = "\033[10;15H"
cursor_pos_3 = "\033[40;20H"
cursor_pos_4 = "\033[25;60H"
cursor_pos_5 = "\033[5;30H"
cursor_pos_6 = "\033[12;12H"
cursor_pos_7 = "\033[70;10H"
cursor_pos_8 = "\033[15;45H"
cursor_pos_9 = "\033[1;80H"
cursor_pos_10 = "\033[50;25H"
# Erasing
erase_line = "\033[2K"
erase_to_end = "\033[K"
erase_screen = "\033[2J"
erase_screen_from_cursor = "\033[J"
# Saving and restoring cursor
save_cursor = "\033[s"
restore_cursor = "\033[u"
# Alternative screen buffer
alt_screen_on = "\033[?1049h"
alt_screen_off = "\033[?1049l"
for name in locals():
val = "]"+locals()[ name ]+"["
out = strip_ansi_sequences( val )
print( f"{name+":":<25} output: {out!r}, input: {val!r}" )
--- Synchronet 3.21a-Linux NewsLink 1.2