Removing ANSI color codes from text stream
up vote
43
down vote
favorite
Examining the output from
perl -e 'use Term::ANSIColor; print color "white"; print "ABCn"; print color "reset";'
in a text editor (e.g., vi
) shows the following:
^[[37mABC
^[[0m
How would one remove the ANSI color codes from the output file? I suppose the best way would be to pipe the output through a stream editor of sorts.
The following does not work
perl -e 'use Term::ANSIColor; print color "white"; print "ABCn"; print color "reset";' | perl -pe 's/^[[37m//g' | perl -pe 's/^[[0m//g'
regex sed perl awk
add a comment |
up vote
43
down vote
favorite
Examining the output from
perl -e 'use Term::ANSIColor; print color "white"; print "ABCn"; print color "reset";'
in a text editor (e.g., vi
) shows the following:
^[[37mABC
^[[0m
How would one remove the ANSI color codes from the output file? I suppose the best way would be to pipe the output through a stream editor of sorts.
The following does not work
perl -e 'use Term::ANSIColor; print color "white"; print "ABCn"; print color "reset";' | perl -pe 's/^[[37m//g' | perl -pe 's/^[[0m//g'
regex sed perl awk
Not an answer to the question, but you can also pipe the output tomore
orless -R
which can interpret the escape codes as color instead of a text editor.
– terdon
Jul 3 '13 at 13:50
add a comment |
up vote
43
down vote
favorite
up vote
43
down vote
favorite
Examining the output from
perl -e 'use Term::ANSIColor; print color "white"; print "ABCn"; print color "reset";'
in a text editor (e.g., vi
) shows the following:
^[[37mABC
^[[0m
How would one remove the ANSI color codes from the output file? I suppose the best way would be to pipe the output through a stream editor of sorts.
The following does not work
perl -e 'use Term::ANSIColor; print color "white"; print "ABCn"; print color "reset";' | perl -pe 's/^[[37m//g' | perl -pe 's/^[[0m//g'
regex sed perl awk
Examining the output from
perl -e 'use Term::ANSIColor; print color "white"; print "ABCn"; print color "reset";'
in a text editor (e.g., vi
) shows the following:
^[[37mABC
^[[0m
How would one remove the ANSI color codes from the output file? I suppose the best way would be to pipe the output through a stream editor of sorts.
The following does not work
perl -e 'use Term::ANSIColor; print color "white"; print "ABCn"; print color "reset";' | perl -pe 's/^[[37m//g' | perl -pe 's/^[[0m//g'
regex sed perl awk
regex sed perl awk
asked Jan 21 '12 at 1:01
user001
87541427
87541427
Not an answer to the question, but you can also pipe the output tomore
orless -R
which can interpret the escape codes as color instead of a text editor.
– terdon
Jul 3 '13 at 13:50
add a comment |
Not an answer to the question, but you can also pipe the output tomore
orless -R
which can interpret the escape codes as color instead of a text editor.
– terdon
Jul 3 '13 at 13:50
Not an answer to the question, but you can also pipe the output to
more
or less -R
which can interpret the escape codes as color instead of a text editor.– terdon
Jul 3 '13 at 13:50
Not an answer to the question, but you can also pipe the output to
more
or less -R
which can interpret the escape codes as color instead of a text editor.– terdon
Jul 3 '13 at 13:50
add a comment |
8 Answers
8
active
oldest
votes
up vote
60
down vote
accepted
The characters ^[[37m
and ^[[0m
are part of the ANSI Escape sequences (CSI codes).
See also the complete specifications.
Using sed
sed 's/x1b[[0-9;]*m//g'
x1b
is the escape special character (same asx1B
or '33`)
[
is the second character of the escape sequence
[0-9;]*
is the color value(s)
m
is the last character of the escape sequence
Example with OP's command line:
(OP = Original Poster)
perl -e 'use Term::ANSIColor; print color "white"; print "ABCn"; print color "reset";' |
sed 's/x1b[[0-9;]*m//g'
Tom Hale suggests to remove all other escape sequences using [a-zA-Z]
instead just the letter m
specific to color escape sequence. But [a-zA-Z]
may be too wide and could remove too much. Michał Faleński and Miguel Mota proposes to remove only some escape sequences using [mGKH]
and [mGKF]
respectively.
sed 's/x1b[[0-9;]*m//g' # Remove color sequences only
sed 's/x1b[[0-9;]*[a-zA-Z]//g' # Remove all escape sequences
sed 's/x1b[[0-9;]*[mGKH]//g' # Remove color and move sequences
sed 's/x1b[[0-9;]*[mGKF]//g' # Remove color and move sequences
Last escape
sequence
character Purpose
--------- -------------------------------
m Color
G Horizontal cursor move
K Horizontal deletion
H New cursor position
F Move cursor to previous n lines
Using perl
The version of sed
installed on some operating systems may be limited (e.g. MacOS X). The command perl
has the advantage to be often more easily to install/update on more operating systems.
Choose your regex depending on how much commands you want to filter:
perl -pe 's/x1b[[0-9;]*m//g' # Remove colors only
perl -pe 's/x1b[[0-9;]*[mG]//g'
perl -pe 's/x1b[[0-9;]*[mGKH]//g'
perl -pe 's/x1b[[0-9;]*[a-zA-Z]//g'
Example with OP's command line:
perl -e 'use Term::ANSIColor; print color "white"; print "ABCn"; print color "reset";' |
perl -pe 's/x1b[[0-9;]*m//g'
Usage
As pointed out by Stuart Cardall's comment, this trick is used by the project Ultimate Nginx Bad Bot (almost 1000 stars) to clean up the email report ;-)
1
Thanks for thesed
command and the explanation. :)
– Redsandro
Feb 5 '13 at 14:15
2
Some color codes (e.g. Linux terminal) contain a prefix, e.g.1;31m
so better add;
to your regex:cat colored.log | sed -r 's/x1b[[0-9;]*m//g'
or they won't be stripped.
– Redsandro
Mar 3 '14 at 13:11
1
this is great used it in github.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/blob/… to clean up the email report.
– Stuart Cardall
Jun 7 '17 at 18:59
1
In your perl example you have a command that filters out the colours. But what are the other commands? They additionally filter out themG
andmGKH
and thena-zA-Z
, can you add a comment next to each one?
– CMCDragonkai
Nov 5 at 3:10
Thank you @CMCDragonkai for your feedback. I have provided an enhanced answer. Cheers ;-)
– olibre
Nov 22 at 9:45
add a comment |
up vote
16
down vote
I have found out a better escape sequence remover.
Check this:
perl -pe 's/x1b[[0-9;]*[mG]//g'
1
What's the improvement from the accepted answer (superuser.com/a/380778/46794)?
– Blaisorblade
Sep 30 '16 at 20:57
2
@Blaisorblade It works on OS X, whereassed -r
does NOT.
– BVengerov
Feb 2 '17 at 16:02
add a comment |
up vote
8
down vote
What is displayed as ^[
is not ^
and [
; it is the ASCII ESC
character, produced by Esc or Ctrl[ (the ^
notation means the Ctrl key).
ESC
is 0x1B hexadecimal or 033 octal, so you have to use x1B
or 33
in your regexes:
perl -pe 's/33[37m//g; s/33[0m//g'
perl -pe 's/33[d*(;d*)*m//g'
add a comment |
up vote
4
down vote
If you prefer something simple you could use the strip-ansi module (Node.js required):
$ npm install --global strip-ansi-cli
Then use it like this:
$ strip-ansi < colors.o
Or just pass in a string:
$ strip-ansi '^[[37mABC^[[0m'
This a useless use ofcat
(UUOC) — it should be possible to dostrip-ansi colors.o
or at leaststrip-ansi < colors.o
.
– Scott
Feb 11 '16 at 5:36
1
@Scott Sure, you can also dostrip-ansi < colors.o
, but from experience people are more familiar with piping. I've updated the answer.
– Sindre Sorhus
Feb 11 '16 at 9:09
add a comment |
up vote
2
down vote
The "answered" question didn't work for me, so I created this regex instead to remove the escape sequences produced by the perl Term::ANSIColor module.
cat colors.o | perl -pe 's/x1b[[^m]+m//g;
Grawity's regex should work fine, but using +'s appears to work ok too.
2
(1) What do you mean byThe "answered" question
? Do you mean the accepted answer? (2) This command does not work — it does not even execute — because it has an unmatched (unbalanced) quote. (3) This a useless use ofcat
(UUOC) — it should be possible to doperl -pe
command
colors.o
. (4) Who ever said anything about the codes being in a.o
file?
– Scott
Feb 11 '16 at 5:35
add a comment |
up vote
2
down vote
commandlinefu gives this answer which strips ANSI colours as well as movement commands:
sed "s,x1B[[0-9;]*[a-zA-Z],,g"
For just colours, you want:
sed "s,x1B[[0-9;]*m,,g"
add a comment |
up vote
0
down vote
I had similar problem with removing characters added from collecting interactive top output via putty and this helped:
cat putty1.log | perl -pe 's/x1b.*?[mGKH]//g'
3
This a useless use ofcat
(UUOC) — it should be possible to doperl -pe
command
putty1.log
.
– Scott
Feb 11 '16 at 5:36
add a comment |
up vote
0
down vote
This is what worked for me (tested on Mac OS X)
perl -pe 's/[[0-9;]*[mGKF]//g'
add a comment |
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
60
down vote
accepted
The characters ^[[37m
and ^[[0m
are part of the ANSI Escape sequences (CSI codes).
See also the complete specifications.
Using sed
sed 's/x1b[[0-9;]*m//g'
x1b
is the escape special character (same asx1B
or '33`)
[
is the second character of the escape sequence
[0-9;]*
is the color value(s)
m
is the last character of the escape sequence
Example with OP's command line:
(OP = Original Poster)
perl -e 'use Term::ANSIColor; print color "white"; print "ABCn"; print color "reset";' |
sed 's/x1b[[0-9;]*m//g'
Tom Hale suggests to remove all other escape sequences using [a-zA-Z]
instead just the letter m
specific to color escape sequence. But [a-zA-Z]
may be too wide and could remove too much. Michał Faleński and Miguel Mota proposes to remove only some escape sequences using [mGKH]
and [mGKF]
respectively.
sed 's/x1b[[0-9;]*m//g' # Remove color sequences only
sed 's/x1b[[0-9;]*[a-zA-Z]//g' # Remove all escape sequences
sed 's/x1b[[0-9;]*[mGKH]//g' # Remove color and move sequences
sed 's/x1b[[0-9;]*[mGKF]//g' # Remove color and move sequences
Last escape
sequence
character Purpose
--------- -------------------------------
m Color
G Horizontal cursor move
K Horizontal deletion
H New cursor position
F Move cursor to previous n lines
Using perl
The version of sed
installed on some operating systems may be limited (e.g. MacOS X). The command perl
has the advantage to be often more easily to install/update on more operating systems.
Choose your regex depending on how much commands you want to filter:
perl -pe 's/x1b[[0-9;]*m//g' # Remove colors only
perl -pe 's/x1b[[0-9;]*[mG]//g'
perl -pe 's/x1b[[0-9;]*[mGKH]//g'
perl -pe 's/x1b[[0-9;]*[a-zA-Z]//g'
Example with OP's command line:
perl -e 'use Term::ANSIColor; print color "white"; print "ABCn"; print color "reset";' |
perl -pe 's/x1b[[0-9;]*m//g'
Usage
As pointed out by Stuart Cardall's comment, this trick is used by the project Ultimate Nginx Bad Bot (almost 1000 stars) to clean up the email report ;-)
1
Thanks for thesed
command and the explanation. :)
– Redsandro
Feb 5 '13 at 14:15
2
Some color codes (e.g. Linux terminal) contain a prefix, e.g.1;31m
so better add;
to your regex:cat colored.log | sed -r 's/x1b[[0-9;]*m//g'
or they won't be stripped.
– Redsandro
Mar 3 '14 at 13:11
1
this is great used it in github.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/blob/… to clean up the email report.
– Stuart Cardall
Jun 7 '17 at 18:59
1
In your perl example you have a command that filters out the colours. But what are the other commands? They additionally filter out themG
andmGKH
and thena-zA-Z
, can you add a comment next to each one?
– CMCDragonkai
Nov 5 at 3:10
Thank you @CMCDragonkai for your feedback. I have provided an enhanced answer. Cheers ;-)
– olibre
Nov 22 at 9:45
add a comment |
up vote
60
down vote
accepted
The characters ^[[37m
and ^[[0m
are part of the ANSI Escape sequences (CSI codes).
See also the complete specifications.
Using sed
sed 's/x1b[[0-9;]*m//g'
x1b
is the escape special character (same asx1B
or '33`)
[
is the second character of the escape sequence
[0-9;]*
is the color value(s)
m
is the last character of the escape sequence
Example with OP's command line:
(OP = Original Poster)
perl -e 'use Term::ANSIColor; print color "white"; print "ABCn"; print color "reset";' |
sed 's/x1b[[0-9;]*m//g'
Tom Hale suggests to remove all other escape sequences using [a-zA-Z]
instead just the letter m
specific to color escape sequence. But [a-zA-Z]
may be too wide and could remove too much. Michał Faleński and Miguel Mota proposes to remove only some escape sequences using [mGKH]
and [mGKF]
respectively.
sed 's/x1b[[0-9;]*m//g' # Remove color sequences only
sed 's/x1b[[0-9;]*[a-zA-Z]//g' # Remove all escape sequences
sed 's/x1b[[0-9;]*[mGKH]//g' # Remove color and move sequences
sed 's/x1b[[0-9;]*[mGKF]//g' # Remove color and move sequences
Last escape
sequence
character Purpose
--------- -------------------------------
m Color
G Horizontal cursor move
K Horizontal deletion
H New cursor position
F Move cursor to previous n lines
Using perl
The version of sed
installed on some operating systems may be limited (e.g. MacOS X). The command perl
has the advantage to be often more easily to install/update on more operating systems.
Choose your regex depending on how much commands you want to filter:
perl -pe 's/x1b[[0-9;]*m//g' # Remove colors only
perl -pe 's/x1b[[0-9;]*[mG]//g'
perl -pe 's/x1b[[0-9;]*[mGKH]//g'
perl -pe 's/x1b[[0-9;]*[a-zA-Z]//g'
Example with OP's command line:
perl -e 'use Term::ANSIColor; print color "white"; print "ABCn"; print color "reset";' |
perl -pe 's/x1b[[0-9;]*m//g'
Usage
As pointed out by Stuart Cardall's comment, this trick is used by the project Ultimate Nginx Bad Bot (almost 1000 stars) to clean up the email report ;-)
1
Thanks for thesed
command and the explanation. :)
– Redsandro
Feb 5 '13 at 14:15
2
Some color codes (e.g. Linux terminal) contain a prefix, e.g.1;31m
so better add;
to your regex:cat colored.log | sed -r 's/x1b[[0-9;]*m//g'
or they won't be stripped.
– Redsandro
Mar 3 '14 at 13:11
1
this is great used it in github.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/blob/… to clean up the email report.
– Stuart Cardall
Jun 7 '17 at 18:59
1
In your perl example you have a command that filters out the colours. But what are the other commands? They additionally filter out themG
andmGKH
and thena-zA-Z
, can you add a comment next to each one?
– CMCDragonkai
Nov 5 at 3:10
Thank you @CMCDragonkai for your feedback. I have provided an enhanced answer. Cheers ;-)
– olibre
Nov 22 at 9:45
add a comment |
up vote
60
down vote
accepted
up vote
60
down vote
accepted
The characters ^[[37m
and ^[[0m
are part of the ANSI Escape sequences (CSI codes).
See also the complete specifications.
Using sed
sed 's/x1b[[0-9;]*m//g'
x1b
is the escape special character (same asx1B
or '33`)
[
is the second character of the escape sequence
[0-9;]*
is the color value(s)
m
is the last character of the escape sequence
Example with OP's command line:
(OP = Original Poster)
perl -e 'use Term::ANSIColor; print color "white"; print "ABCn"; print color "reset";' |
sed 's/x1b[[0-9;]*m//g'
Tom Hale suggests to remove all other escape sequences using [a-zA-Z]
instead just the letter m
specific to color escape sequence. But [a-zA-Z]
may be too wide and could remove too much. Michał Faleński and Miguel Mota proposes to remove only some escape sequences using [mGKH]
and [mGKF]
respectively.
sed 's/x1b[[0-9;]*m//g' # Remove color sequences only
sed 's/x1b[[0-9;]*[a-zA-Z]//g' # Remove all escape sequences
sed 's/x1b[[0-9;]*[mGKH]//g' # Remove color and move sequences
sed 's/x1b[[0-9;]*[mGKF]//g' # Remove color and move sequences
Last escape
sequence
character Purpose
--------- -------------------------------
m Color
G Horizontal cursor move
K Horizontal deletion
H New cursor position
F Move cursor to previous n lines
Using perl
The version of sed
installed on some operating systems may be limited (e.g. MacOS X). The command perl
has the advantage to be often more easily to install/update on more operating systems.
Choose your regex depending on how much commands you want to filter:
perl -pe 's/x1b[[0-9;]*m//g' # Remove colors only
perl -pe 's/x1b[[0-9;]*[mG]//g'
perl -pe 's/x1b[[0-9;]*[mGKH]//g'
perl -pe 's/x1b[[0-9;]*[a-zA-Z]//g'
Example with OP's command line:
perl -e 'use Term::ANSIColor; print color "white"; print "ABCn"; print color "reset";' |
perl -pe 's/x1b[[0-9;]*m//g'
Usage
As pointed out by Stuart Cardall's comment, this trick is used by the project Ultimate Nginx Bad Bot (almost 1000 stars) to clean up the email report ;-)
The characters ^[[37m
and ^[[0m
are part of the ANSI Escape sequences (CSI codes).
See also the complete specifications.
Using sed
sed 's/x1b[[0-9;]*m//g'
x1b
is the escape special character (same asx1B
or '33`)
[
is the second character of the escape sequence
[0-9;]*
is the color value(s)
m
is the last character of the escape sequence
Example with OP's command line:
(OP = Original Poster)
perl -e 'use Term::ANSIColor; print color "white"; print "ABCn"; print color "reset";' |
sed 's/x1b[[0-9;]*m//g'
Tom Hale suggests to remove all other escape sequences using [a-zA-Z]
instead just the letter m
specific to color escape sequence. But [a-zA-Z]
may be too wide and could remove too much. Michał Faleński and Miguel Mota proposes to remove only some escape sequences using [mGKH]
and [mGKF]
respectively.
sed 's/x1b[[0-9;]*m//g' # Remove color sequences only
sed 's/x1b[[0-9;]*[a-zA-Z]//g' # Remove all escape sequences
sed 's/x1b[[0-9;]*[mGKH]//g' # Remove color and move sequences
sed 's/x1b[[0-9;]*[mGKF]//g' # Remove color and move sequences
Last escape
sequence
character Purpose
--------- -------------------------------
m Color
G Horizontal cursor move
K Horizontal deletion
H New cursor position
F Move cursor to previous n lines
Using perl
The version of sed
installed on some operating systems may be limited (e.g. MacOS X). The command perl
has the advantage to be often more easily to install/update on more operating systems.
Choose your regex depending on how much commands you want to filter:
perl -pe 's/x1b[[0-9;]*m//g' # Remove colors only
perl -pe 's/x1b[[0-9;]*[mG]//g'
perl -pe 's/x1b[[0-9;]*[mGKH]//g'
perl -pe 's/x1b[[0-9;]*[a-zA-Z]//g'
Example with OP's command line:
perl -e 'use Term::ANSIColor; print color "white"; print "ABCn"; print color "reset";' |
perl -pe 's/x1b[[0-9;]*m//g'
Usage
As pointed out by Stuart Cardall's comment, this trick is used by the project Ultimate Nginx Bad Bot (almost 1000 stars) to clean up the email report ;-)
edited Nov 21 at 17:07
answered Jan 21 '12 at 1:40
olibre
997911
997911
1
Thanks for thesed
command and the explanation. :)
– Redsandro
Feb 5 '13 at 14:15
2
Some color codes (e.g. Linux terminal) contain a prefix, e.g.1;31m
so better add;
to your regex:cat colored.log | sed -r 's/x1b[[0-9;]*m//g'
or they won't be stripped.
– Redsandro
Mar 3 '14 at 13:11
1
this is great used it in github.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/blob/… to clean up the email report.
– Stuart Cardall
Jun 7 '17 at 18:59
1
In your perl example you have a command that filters out the colours. But what are the other commands? They additionally filter out themG
andmGKH
and thena-zA-Z
, can you add a comment next to each one?
– CMCDragonkai
Nov 5 at 3:10
Thank you @CMCDragonkai for your feedback. I have provided an enhanced answer. Cheers ;-)
– olibre
Nov 22 at 9:45
add a comment |
1
Thanks for thesed
command and the explanation. :)
– Redsandro
Feb 5 '13 at 14:15
2
Some color codes (e.g. Linux terminal) contain a prefix, e.g.1;31m
so better add;
to your regex:cat colored.log | sed -r 's/x1b[[0-9;]*m//g'
or they won't be stripped.
– Redsandro
Mar 3 '14 at 13:11
1
this is great used it in github.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/blob/… to clean up the email report.
– Stuart Cardall
Jun 7 '17 at 18:59
1
In your perl example you have a command that filters out the colours. But what are the other commands? They additionally filter out themG
andmGKH
and thena-zA-Z
, can you add a comment next to each one?
– CMCDragonkai
Nov 5 at 3:10
Thank you @CMCDragonkai for your feedback. I have provided an enhanced answer. Cheers ;-)
– olibre
Nov 22 at 9:45
1
1
Thanks for the
sed
command and the explanation. :)– Redsandro
Feb 5 '13 at 14:15
Thanks for the
sed
command and the explanation. :)– Redsandro
Feb 5 '13 at 14:15
2
2
Some color codes (e.g. Linux terminal) contain a prefix, e.g.
1;31m
so better add ;
to your regex: cat colored.log | sed -r 's/x1b[[0-9;]*m//g'
or they won't be stripped.– Redsandro
Mar 3 '14 at 13:11
Some color codes (e.g. Linux terminal) contain a prefix, e.g.
1;31m
so better add ;
to your regex: cat colored.log | sed -r 's/x1b[[0-9;]*m//g'
or they won't be stripped.– Redsandro
Mar 3 '14 at 13:11
1
1
this is great used it in github.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/blob/… to clean up the email report.
– Stuart Cardall
Jun 7 '17 at 18:59
this is great used it in github.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/blob/… to clean up the email report.
– Stuart Cardall
Jun 7 '17 at 18:59
1
1
In your perl example you have a command that filters out the colours. But what are the other commands? They additionally filter out the
mG
and mGKH
and then a-zA-Z
, can you add a comment next to each one?– CMCDragonkai
Nov 5 at 3:10
In your perl example you have a command that filters out the colours. But what are the other commands? They additionally filter out the
mG
and mGKH
and then a-zA-Z
, can you add a comment next to each one?– CMCDragonkai
Nov 5 at 3:10
Thank you @CMCDragonkai for your feedback. I have provided an enhanced answer. Cheers ;-)
– olibre
Nov 22 at 9:45
Thank you @CMCDragonkai for your feedback. I have provided an enhanced answer. Cheers ;-)
– olibre
Nov 22 at 9:45
add a comment |
up vote
16
down vote
I have found out a better escape sequence remover.
Check this:
perl -pe 's/x1b[[0-9;]*[mG]//g'
1
What's the improvement from the accepted answer (superuser.com/a/380778/46794)?
– Blaisorblade
Sep 30 '16 at 20:57
2
@Blaisorblade It works on OS X, whereassed -r
does NOT.
– BVengerov
Feb 2 '17 at 16:02
add a comment |
up vote
16
down vote
I have found out a better escape sequence remover.
Check this:
perl -pe 's/x1b[[0-9;]*[mG]//g'
1
What's the improvement from the accepted answer (superuser.com/a/380778/46794)?
– Blaisorblade
Sep 30 '16 at 20:57
2
@Blaisorblade It works on OS X, whereassed -r
does NOT.
– BVengerov
Feb 2 '17 at 16:02
add a comment |
up vote
16
down vote
up vote
16
down vote
I have found out a better escape sequence remover.
Check this:
perl -pe 's/x1b[[0-9;]*[mG]//g'
I have found out a better escape sequence remover.
Check this:
perl -pe 's/x1b[[0-9;]*[mG]//g'
edited Mar 5 '13 at 13:55
mta
1,3272818
1,3272818
answered Mar 5 '13 at 13:24
user204331
16112
16112
1
What's the improvement from the accepted answer (superuser.com/a/380778/46794)?
– Blaisorblade
Sep 30 '16 at 20:57
2
@Blaisorblade It works on OS X, whereassed -r
does NOT.
– BVengerov
Feb 2 '17 at 16:02
add a comment |
1
What's the improvement from the accepted answer (superuser.com/a/380778/46794)?
– Blaisorblade
Sep 30 '16 at 20:57
2
@Blaisorblade It works on OS X, whereassed -r
does NOT.
– BVengerov
Feb 2 '17 at 16:02
1
1
What's the improvement from the accepted answer (superuser.com/a/380778/46794)?
– Blaisorblade
Sep 30 '16 at 20:57
What's the improvement from the accepted answer (superuser.com/a/380778/46794)?
– Blaisorblade
Sep 30 '16 at 20:57
2
2
@Blaisorblade It works on OS X, whereas
sed -r
does NOT.– BVengerov
Feb 2 '17 at 16:02
@Blaisorblade It works on OS X, whereas
sed -r
does NOT.– BVengerov
Feb 2 '17 at 16:02
add a comment |
up vote
8
down vote
What is displayed as ^[
is not ^
and [
; it is the ASCII ESC
character, produced by Esc or Ctrl[ (the ^
notation means the Ctrl key).
ESC
is 0x1B hexadecimal or 033 octal, so you have to use x1B
or 33
in your regexes:
perl -pe 's/33[37m//g; s/33[0m//g'
perl -pe 's/33[d*(;d*)*m//g'
add a comment |
up vote
8
down vote
What is displayed as ^[
is not ^
and [
; it is the ASCII ESC
character, produced by Esc or Ctrl[ (the ^
notation means the Ctrl key).
ESC
is 0x1B hexadecimal or 033 octal, so you have to use x1B
or 33
in your regexes:
perl -pe 's/33[37m//g; s/33[0m//g'
perl -pe 's/33[d*(;d*)*m//g'
add a comment |
up vote
8
down vote
up vote
8
down vote
What is displayed as ^[
is not ^
and [
; it is the ASCII ESC
character, produced by Esc or Ctrl[ (the ^
notation means the Ctrl key).
ESC
is 0x1B hexadecimal or 033 octal, so you have to use x1B
or 33
in your regexes:
perl -pe 's/33[37m//g; s/33[0m//g'
perl -pe 's/33[d*(;d*)*m//g'
What is displayed as ^[
is not ^
and [
; it is the ASCII ESC
character, produced by Esc or Ctrl[ (the ^
notation means the Ctrl key).
ESC
is 0x1B hexadecimal or 033 octal, so you have to use x1B
or 33
in your regexes:
perl -pe 's/33[37m//g; s/33[0m//g'
perl -pe 's/33[d*(;d*)*m//g'
edited Jan 21 '12 at 3:27
user001
87541427
87541427
answered Jan 21 '12 at 1:39
grawity
230k35483542
230k35483542
add a comment |
add a comment |
up vote
4
down vote
If you prefer something simple you could use the strip-ansi module (Node.js required):
$ npm install --global strip-ansi-cli
Then use it like this:
$ strip-ansi < colors.o
Or just pass in a string:
$ strip-ansi '^[[37mABC^[[0m'
This a useless use ofcat
(UUOC) — it should be possible to dostrip-ansi colors.o
or at leaststrip-ansi < colors.o
.
– Scott
Feb 11 '16 at 5:36
1
@Scott Sure, you can also dostrip-ansi < colors.o
, but from experience people are more familiar with piping. I've updated the answer.
– Sindre Sorhus
Feb 11 '16 at 9:09
add a comment |
up vote
4
down vote
If you prefer something simple you could use the strip-ansi module (Node.js required):
$ npm install --global strip-ansi-cli
Then use it like this:
$ strip-ansi < colors.o
Or just pass in a string:
$ strip-ansi '^[[37mABC^[[0m'
This a useless use ofcat
(UUOC) — it should be possible to dostrip-ansi colors.o
or at leaststrip-ansi < colors.o
.
– Scott
Feb 11 '16 at 5:36
1
@Scott Sure, you can also dostrip-ansi < colors.o
, but from experience people are more familiar with piping. I've updated the answer.
– Sindre Sorhus
Feb 11 '16 at 9:09
add a comment |
up vote
4
down vote
up vote
4
down vote
If you prefer something simple you could use the strip-ansi module (Node.js required):
$ npm install --global strip-ansi-cli
Then use it like this:
$ strip-ansi < colors.o
Or just pass in a string:
$ strip-ansi '^[[37mABC^[[0m'
If you prefer something simple you could use the strip-ansi module (Node.js required):
$ npm install --global strip-ansi-cli
Then use it like this:
$ strip-ansi < colors.o
Or just pass in a string:
$ strip-ansi '^[[37mABC^[[0m'
edited Feb 11 '16 at 9:08
answered Jul 4 '14 at 12:44
Sindre Sorhus
2741215
2741215
This a useless use ofcat
(UUOC) — it should be possible to dostrip-ansi colors.o
or at leaststrip-ansi < colors.o
.
– Scott
Feb 11 '16 at 5:36
1
@Scott Sure, you can also dostrip-ansi < colors.o
, but from experience people are more familiar with piping. I've updated the answer.
– Sindre Sorhus
Feb 11 '16 at 9:09
add a comment |
This a useless use ofcat
(UUOC) — it should be possible to dostrip-ansi colors.o
or at leaststrip-ansi < colors.o
.
– Scott
Feb 11 '16 at 5:36
1
@Scott Sure, you can also dostrip-ansi < colors.o
, but from experience people are more familiar with piping. I've updated the answer.
– Sindre Sorhus
Feb 11 '16 at 9:09
This a useless use of
cat
(UUOC) — it should be possible to do strip-ansi colors.o
or at least strip-ansi < colors.o
.– Scott
Feb 11 '16 at 5:36
This a useless use of
cat
(UUOC) — it should be possible to do strip-ansi colors.o
or at least strip-ansi < colors.o
.– Scott
Feb 11 '16 at 5:36
1
1
@Scott Sure, you can also do
strip-ansi < colors.o
, but from experience people are more familiar with piping. I've updated the answer.– Sindre Sorhus
Feb 11 '16 at 9:09
@Scott Sure, you can also do
strip-ansi < colors.o
, but from experience people are more familiar with piping. I've updated the answer.– Sindre Sorhus
Feb 11 '16 at 9:09
add a comment |
up vote
2
down vote
The "answered" question didn't work for me, so I created this regex instead to remove the escape sequences produced by the perl Term::ANSIColor module.
cat colors.o | perl -pe 's/x1b[[^m]+m//g;
Grawity's regex should work fine, but using +'s appears to work ok too.
2
(1) What do you mean byThe "answered" question
? Do you mean the accepted answer? (2) This command does not work — it does not even execute — because it has an unmatched (unbalanced) quote. (3) This a useless use ofcat
(UUOC) — it should be possible to doperl -pe
command
colors.o
. (4) Who ever said anything about the codes being in a.o
file?
– Scott
Feb 11 '16 at 5:35
add a comment |
up vote
2
down vote
The "answered" question didn't work for me, so I created this regex instead to remove the escape sequences produced by the perl Term::ANSIColor module.
cat colors.o | perl -pe 's/x1b[[^m]+m//g;
Grawity's regex should work fine, but using +'s appears to work ok too.
2
(1) What do you mean byThe "answered" question
? Do you mean the accepted answer? (2) This command does not work — it does not even execute — because it has an unmatched (unbalanced) quote. (3) This a useless use ofcat
(UUOC) — it should be possible to doperl -pe
command
colors.o
. (4) Who ever said anything about the codes being in a.o
file?
– Scott
Feb 11 '16 at 5:35
add a comment |
up vote
2
down vote
up vote
2
down vote
The "answered" question didn't work for me, so I created this regex instead to remove the escape sequences produced by the perl Term::ANSIColor module.
cat colors.o | perl -pe 's/x1b[[^m]+m//g;
Grawity's regex should work fine, but using +'s appears to work ok too.
The "answered" question didn't work for me, so I created this regex instead to remove the escape sequences produced by the perl Term::ANSIColor module.
cat colors.o | perl -pe 's/x1b[[^m]+m//g;
Grawity's regex should work fine, but using +'s appears to work ok too.
edited Mar 13 '13 at 22:14
answered Mar 13 '13 at 22:08
castl3bravo
212
212
2
(1) What do you mean byThe "answered" question
? Do you mean the accepted answer? (2) This command does not work — it does not even execute — because it has an unmatched (unbalanced) quote. (3) This a useless use ofcat
(UUOC) — it should be possible to doperl -pe
command
colors.o
. (4) Who ever said anything about the codes being in a.o
file?
– Scott
Feb 11 '16 at 5:35
add a comment |
2
(1) What do you mean byThe "answered" question
? Do you mean the accepted answer? (2) This command does not work — it does not even execute — because it has an unmatched (unbalanced) quote. (3) This a useless use ofcat
(UUOC) — it should be possible to doperl -pe
command
colors.o
. (4) Who ever said anything about the codes being in a.o
file?
– Scott
Feb 11 '16 at 5:35
2
2
(1) What do you mean by
The "answered" question
? Do you mean the accepted answer? (2) This command does not work — it does not even execute — because it has an unmatched (unbalanced) quote. (3) This a useless use of cat
(UUOC) — it should be possible to do perl -pe
command
colors.o
. (4) Who ever said anything about the codes being in a .o
file?– Scott
Feb 11 '16 at 5:35
(1) What do you mean by
The "answered" question
? Do you mean the accepted answer? (2) This command does not work — it does not even execute — because it has an unmatched (unbalanced) quote. (3) This a useless use of cat
(UUOC) — it should be possible to do perl -pe
command
colors.o
. (4) Who ever said anything about the codes being in a .o
file?– Scott
Feb 11 '16 at 5:35
add a comment |
up vote
2
down vote
commandlinefu gives this answer which strips ANSI colours as well as movement commands:
sed "s,x1B[[0-9;]*[a-zA-Z],,g"
For just colours, you want:
sed "s,x1B[[0-9;]*m,,g"
add a comment |
up vote
2
down vote
commandlinefu gives this answer which strips ANSI colours as well as movement commands:
sed "s,x1B[[0-9;]*[a-zA-Z],,g"
For just colours, you want:
sed "s,x1B[[0-9;]*m,,g"
add a comment |
up vote
2
down vote
up vote
2
down vote
commandlinefu gives this answer which strips ANSI colours as well as movement commands:
sed "s,x1B[[0-9;]*[a-zA-Z],,g"
For just colours, you want:
sed "s,x1B[[0-9;]*m,,g"
commandlinefu gives this answer which strips ANSI colours as well as movement commands:
sed "s,x1B[[0-9;]*[a-zA-Z],,g"
For just colours, you want:
sed "s,x1B[[0-9;]*m,,g"
answered Apr 26 '17 at 7:40
Tom Hale
794721
794721
add a comment |
add a comment |
up vote
0
down vote
I had similar problem with removing characters added from collecting interactive top output via putty and this helped:
cat putty1.log | perl -pe 's/x1b.*?[mGKH]//g'
3
This a useless use ofcat
(UUOC) — it should be possible to doperl -pe
command
putty1.log
.
– Scott
Feb 11 '16 at 5:36
add a comment |
up vote
0
down vote
I had similar problem with removing characters added from collecting interactive top output via putty and this helped:
cat putty1.log | perl -pe 's/x1b.*?[mGKH]//g'
3
This a useless use ofcat
(UUOC) — it should be possible to doperl -pe
command
putty1.log
.
– Scott
Feb 11 '16 at 5:36
add a comment |
up vote
0
down vote
up vote
0
down vote
I had similar problem with removing characters added from collecting interactive top output via putty and this helped:
cat putty1.log | perl -pe 's/x1b.*?[mGKH]//g'
I had similar problem with removing characters added from collecting interactive top output via putty and this helped:
cat putty1.log | perl -pe 's/x1b.*?[mGKH]//g'
answered Jul 3 '13 at 13:45
Michał Faleński
91
91
3
This a useless use ofcat
(UUOC) — it should be possible to doperl -pe
command
putty1.log
.
– Scott
Feb 11 '16 at 5:36
add a comment |
3
This a useless use ofcat
(UUOC) — it should be possible to doperl -pe
command
putty1.log
.
– Scott
Feb 11 '16 at 5:36
3
3
This a useless use of
cat
(UUOC) — it should be possible to do perl -pe
command
putty1.log
.– Scott
Feb 11 '16 at 5:36
This a useless use of
cat
(UUOC) — it should be possible to do perl -pe
command
putty1.log
.– Scott
Feb 11 '16 at 5:36
add a comment |
up vote
0
down vote
This is what worked for me (tested on Mac OS X)
perl -pe 's/[[0-9;]*[mGKF]//g'
add a comment |
up vote
0
down vote
This is what worked for me (tested on Mac OS X)
perl -pe 's/[[0-9;]*[mGKF]//g'
add a comment |
up vote
0
down vote
up vote
0
down vote
This is what worked for me (tested on Mac OS X)
perl -pe 's/[[0-9;]*[mGKF]//g'
This is what worked for me (tested on Mac OS X)
perl -pe 's/[[0-9;]*[mGKF]//g'
answered Sep 16 '17 at 0:01
Miguel Mota
1012
1012
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f380772%2fremoving-ansi-color-codes-from-text-stream%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Not an answer to the question, but you can also pipe the output to
more
orless -R
which can interpret the escape codes as color instead of a text editor.– terdon
Jul 3 '13 at 13:50