Score:0

Strip ANSI sequences from string through bash-python code

in flag

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'
hr flag
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`?
Bruno Henrique Peixoto avatar
in flag
It is very much the beginning. Thanks:)
hr flag
... 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)
pLumo avatar
in flag
... or a separate script file.
pLumo avatar
in flag
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.
Bruno Henrique Peixoto avatar
in flag
I updated the question with some progress. May you further investigate this enigma?
pLumo avatar
in flag
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`
Bruno Henrique Peixoto avatar
in flag
Pip version: pip 22.0.3 from $path (python 3.8) pip3 install strip-ansi: Requirement already satisfied: strip-ansi in $LOCAL_PATH
pLumo avatar
in flag
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 avatar
in flag
You install `strip-ansi`, but use `strip_ansi` with underscore. Documentation has two different versions of their installation instructions ?!
Bruno Henrique Peixoto avatar
in flag
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?
pLumo avatar
in flag
The problem is `echo` , it does not evaluate to ansi codes. Use `echo -e` or `printf` and it works just fine.
Bruno Henrique Peixoto avatar
in flag
GREAT! THank, you, sir. :)
Score:1
in flag

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

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.