0

How to remove control sequence or ANSI color or ANSI Escape codes from grep output of the jupyter notebook file?

This is the sample part of the saved ipynb file.

     "text": [
      "\u001b[32m2023-12-08 00:32:30.319\u001b[0m | \u001b[1mINFO    \u001b[0m | \u001b[36mtimeit\u001b[0m:\u001b[36minner\u001b[0m:\u001b[36m6\u001b[0m - \u001b[1msdfsdfsdf\u001b[0m\n",
]

To create the ipynb file, execute the code in the cell of jupyter.

#install loguru if needed
#%pip install loguru
import timeit

timeit.timeit(setup="from loguru import logger", stmt='''logger.info('sdfsdfsdf')''', number=2)

I use jupyter-notebook6. you can install with python3 -m pip install notebook==6.5.7. you can start the notebook server with jupyter-notebook shell command after installation.

To install loguru lib, execute %pip install loguru in the cell of jupyter-notebook. However, it doesn't mean it's the specific lib's problem. Such a attribute error can show the codes I want to remove.

enter image description here

you can view the text of the ipynb file.

enter image description here

grep -r timeit output is

#zsh grep (bsd)
./python3.ipynb:      "\u001b[32m2023-12-08 00:32:30.319\u001b[0m | \u001b[1mINFO    \u001b[0m | \u001b[36mtimeit\u001b[0m:\u001b[36minner\u001b[0m:\u001b[36m6\u001b[0m - \u001b[1msdfsdfsdf\u001b[0m\n",
#bash grep (gnu)
python3.ipynb:      "\u001b[32m2023-12-08 00:32:30.319\u001b[0m | \u001b[1mINFO    \u001b[0m | \u001b[36mtimeit\u001b[0m:\u001b[36minner\u001b[0m:\u001b[36m6\u001b[0m - \u001b[1msdfsdfsdf\u001b[0m\n",

What I expect is

#zsh grep (bsd)
./python3.ipynb:      "2023-12-08 00:32:30.319 | INFO     | timeit:inner:6 - sdfsdfsdf\n",
#bash grep (gnu)
python3.ipynb:      "2023-12-08 00:32:30.319 | INFO     | timeit:inner:6 - sdfsdfsdf\n",

1 Answers1

0

Unlike the existing answer, (https://superuser.com/a/380778/1752348)

looks like jupyter or something saves the escape code differently. (https://stackoverflow.com/questions/13801273/what-does-u001bj-represent)

So the answer will be

sed 's/\\u001b[[][^A-Za-z]*[A-Za-z]//g'

sed is a 'stream editor for filtering and transforming text'.

In your case,

grep -r timeit | sed 's/\\u001b[[][^A-Za-z]*[A-Za-z]//g'