0

I try to use bash to strip ANSI color escape sequences from a string without success. I tried already some regex-based code.

#!/bin/bash

Blue='\033[0;34m'         # Blue
Clear='\033[0m'           # Text Reset

removeColors (){
    local uncolored_string=''
    
    local import_row='import re; \n'
    local regex_='(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]'
    local func_def_row='def escape_ansi(line): \n'
    local ansi_escape_row="ansi_escape=re.compile(r\'$regex_\') \n" 
    local return_row="return ansi_escape.sub('', line) \n"
    local print_row="print escape_ansi(line = '$1')"

    local code="$import_row$func_def_row$ansi_escape_row$return_row$print_row"
    
    echo $(python -c $code)
}

str="Press ${Blue}any key${Clear} to continue..."
echo -e "$str"

removeColors "$str"

I still receive the code below.

  File "<string>", line 1
    import
         ^
SyntaxError: invalid syntax

Can you help me?

Update:

I found the python library strip-ansi.

removeColors (){
    local uncolored_string=''
    local ansi_snippet="$1"
    
    echo "$(python3 -c "from strip_ansi import strip_ansi; print(strip_ansi(\"$ansi_snippet\"))")"
}

However, even after installing it, I receive the error below:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'strip_ansi'
Bruno Peixoto
  • 271
  • 1
  • 3
  • 11
  • I really don't understand what you're trying to do here, however you'd get the same error from `python -c import`, so perhaps the *first* issue is the unquoted shell variable `$code`? – steeldriver Feb 07 '22 at 00:12
  • It is very much the beginning. Thanks:) – Bruno Peixoto Feb 07 '22 at 01:07
  • ... fwiw I'd suggest using a here document for stuff like this, rather than trying to munge everything into a multiline string. See for example [Hybrid code in shell scripts. Sharing variables](https://unix.stackexchange.com/questions/74244/hybrid-code-in-shell-scripts-sharing-variables) – steeldriver Feb 07 '22 at 01:16
  • ... or a separate script file. – pLumo Feb 07 '22 at 07:48
  • The `\n` is not taken literal here. You could use `$'...'`. However, you have also increment errors for `ansi_escape_row` and `return_row`. And probably other issues as well. Better not to mix shell and python code. – pLumo Feb 07 '22 at 07:56
  • I updated the question with some progress. May you further investigate this enigma? – Bruno Peixoto Feb 07 '22 at 15:13
  • How did you install it? Which `python` version does `pip -V` return. If 2.x, you can try `pip3 install strip_ansi` or `python3 -m pip install strip_ansi` – pLumo Feb 07 '22 at 15:14
  • Pip version: pip 22.0.3 from $path (python 3.8) pip3 install strip-ansi: Requirement already satisfied: strip-ansi in $LOCAL_PATH – Bruno Peixoto Feb 07 '22 at 15:19
  • Btw, why don't you use [`ansi2txt`](https://unix.stackexchange.com/questions/14684/removing-control-chars-including-console-codes-colours-from-script-output/527259#527259) ? – pLumo Feb 07 '22 at 15:19
  • You install `strip-ansi`, but use `strip_ansi` with underscore. Documentation has two different versions of their installation instructions ?! – pLumo Feb 07 '22 at 15:29
  • THe library ansi2txt seemed great, but do not worked for the example: echo "$(echo "Press \033[0;34many key\033[0m to continue..." | ansi2txt | col -b)" May you run it for me, sir? – Bruno Peixoto Feb 07 '22 at 15:37
  • The problem is `echo` , it does not evaluate to ansi codes. Use `echo -e` or `printf` and it works just fine. – pLumo Feb 07 '22 at 15:40
  • GREAT! THank, you, sir. :) – Bruno Peixoto Feb 07 '22 at 15:42

1 Answers1

1

Solution: echo -e "$1" | ansi2txt | col -b

Bruno Peixoto
  • 271
  • 1
  • 3
  • 11