

It was clear that I needed to change that in my. If you're not sure how the input is processed, use -color=auto, which - I believe - causes grep to apply coloring only if its stdout is connected to a terminal.


Never use -color=always, unless you know the output is expected to contain ANSI escape sequences - typically, for human eyeballs on a terminal. bash_profile had the following line: export GREP_OPTIONS='-color=always'Īfter seeing this answer: Different results in grep results when using -color=always option In case anyone else have the same root cause issue, where cat outputs properly to the STDOUT (with colors) but it writes the ANSI Color Codes to file and you want to avoid that completely, this is what worked for me: Problem: cat sometext.txt > ansi_codes_in_file.txt
TEST FOR ANSI ESCAPE SEQUENCES HOW TO
I know there have been many answers on how to deal with the situation once you have files with those characters in it. If you do this, remove these last since they can be involved in longer sequences. This excludes Unicode's higher coded zero-width characters but I believe it's exhaustive for ASCII (Unicode \x00- \xff). I've been using s/+//g, which also includes Delete and Soft Hyphen. The fourth replacement removes the remaining escapes.Īlso consider removing other zero-width ASCII characters such as BEL and other more obscure C0 and C1 control characters. The third replacement is the same thing but also allows Operating System Command sequences to end with a BEL ( \x07, \007, often \a). The second replacement removes the remaining sequences that involve trailing characters and terminate with ST (the String Terminator, Esc \).
TEST FOR ANSI ESCAPE SEQUENCES CODE
The first goes after CSI sequences (escape code sequences that begin with the "Control Sequence Introducer" of Esc [, which covers a lot more than the Select Graphic Rendition sequences that make up the color codes and other text decorations). This perl command, which you can run all on one line if you prefer, has four replacements in it: I'm using it here because it seems more intuitive.) I believe this is an authoritative removal of all ANSI escape sequences: perl -pe note that perl, like many other languages (but not sed), accepts \e as the escape character Esc, \x1b or \033 by code, shown in terminals as ^[. seems col can't keep spaces and tabs as it is. h repace spaces to tab $ echo -e ' ff\tww' | col -b | hdĢ. Update: about col handle tabs and space //mentioned by 〇. Perl -pe 's/\e\*m(?:\e\[K)?//g' # Adam Katz's trickĮxample with OP's command line: perl -e 'use Term::ANSIColor print color "white" print "ABC\n" print color "reset" ' \Īs pointed out by Stuart Cardall's comment, this sed command line is used by the project Ultimate Nginx Bad Bot (1000 stars) to clean up the email report -) Adam Katz suggests to use \e (same as \x1b) in PCRE.Ĭhoose your regex depending on how much commands you want to filter: perl -pe 's/\e\*m//g' # Remove colors only The command perl has the advantage of being generally easier to install/update on more operating systems. The version of sed installed on some operating systems may be limited (e.g. Do not forget to redirect gcc 2>&1 | sed. M Graphics Rendition Mode (including color)īritton Kerin indicates K (in addition to m) removes the colors from gcc error/warning.

Sed 's/\x1b\*//g' # Remove color and move sequences sed 's/\x1b\*//g' # Remove color and move sequences Michał Faleński and Miguel Mota propose to remove only some escape sequences using and respectively. Tom Hale suggests to also remove all other escape sequences using instead of just the letter m specific to the graphics mode escape sequence (color): sed 's/\x1b\*//g' # Remove all escape sequencesīut may be too wide and could remove too much. (OP means Original Poster) perl -e 'use Term::ANSIColor print color "white" print "ABC\n" print color "reset" ' |įlag -e is optional for GNU sed but required for the macOS default sed: sed 's/\x1b\*m//g' # Remove color sequences only The macOS default sed does not support special characters like \e as pointed out by slm and steamer25 in the comments.
