I've checked out a lot of links on how to grep
individual escape characters or literal strings, but I just cannot get them to combine to find the background-red ANSI escape sequence ^[41m
, even typing the ^[
as both Ctrl+V+Ctrl+[ and the two literal characters ^+[ and using both the -E
and -F
flags.
The raw bytes I am trying to find, given by hexdump
are:
1b 5b 33 37 6d 1b 5b 34 31 6d 30 2e 30 30 25
Where this corresponds to WHITE FOREGROUND
RED BACKGROUND
0.00%
. I'm producing these codes with Python's colorama package and Fore.WHITE+Back.RED
, just in case anyone is curious.
So, what is the secret I am missing?
Best Answer
If you use
vim
to open this file, you will know it's not ^[41m, instead it's ^[[41m, which ^[ navigate by arrow key as a set:1b
is Escape represent by single escape character^[
which can be invoked by Ctrl+V follow by Esc.^[
look like 2 characters but it's not, it's single:Do this (Use Ctrl+V follow by Esc to create
^[
, then continuously type\[41m
):Ensure you escape grep by prefix
\
to avoid its alias--color
affect:[Alternative]:
\grep -P '\e\[41m'
(Credit: OP's comment)\grep '^[\[41m'
, which use Ctrl+V follow by Ctrl+[ to create^[
. Useful when backspace in my keyboard is not 0x08, but i can use Ctrl+V follow by Ctrl+H (^H get fromascii 08
) to produce it.