Removing ANSI color codes from text stream











up vote
43
down vote

favorite
18












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'









share|improve this question






















  • 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















up vote
43
down vote

favorite
18












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'









share|improve this question






















  • 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













up vote
43
down vote

favorite
18









up vote
43
down vote

favorite
18






18





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'









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 21 '12 at 1:01









user001

87541427




87541427












  • 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
















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










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 as x1B 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 ;-)






share|improve this answer



















  • 1




    Thanks for the sed 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 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


















up vote
16
down vote













I have found out a better escape sequence remover.
Check this:



perl -pe 's/x1b[[0-9;]*[mG]//g'






share|improve this answer



















  • 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, whereas sed -r does NOT.
    – BVengerov
    Feb 2 '17 at 16:02


















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'





share|improve this answer






























    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'





    share|improve this answer























    • 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




      @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


















    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.






    share|improve this answer



















    • 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


















    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"





    share|improve this answer




























      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'





      share|improve this answer

















      • 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




















      up vote
      0
      down vote













      This is what worked for me (tested on Mac OS X)



      perl -pe 's/[[0-9;]*[mGKF]//g'





      share|improve this answer





















        Your Answer








        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "3"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        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

























        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 as x1B 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 ;-)






        share|improve this answer



















        • 1




          Thanks for the sed 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 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















        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 as x1B 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 ;-)






        share|improve this answer



















        • 1




          Thanks for the sed 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 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













        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 as x1B 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 ;-)






        share|improve this answer














        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 as x1B 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 ;-)







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 at 17:07

























        answered Jan 21 '12 at 1:40









        olibre

        997911




        997911








        • 1




          Thanks for the sed 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 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














        • 1




          Thanks for the sed 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 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








        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












        up vote
        16
        down vote













        I have found out a better escape sequence remover.
        Check this:



        perl -pe 's/x1b[[0-9;]*[mG]//g'






        share|improve this answer



















        • 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, whereas sed -r does NOT.
          – BVengerov
          Feb 2 '17 at 16:02















        up vote
        16
        down vote













        I have found out a better escape sequence remover.
        Check this:



        perl -pe 's/x1b[[0-9;]*[mG]//g'






        share|improve this answer



















        • 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, whereas sed -r does NOT.
          – BVengerov
          Feb 2 '17 at 16:02













        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'






        share|improve this answer














        I have found out a better escape sequence remover.
        Check this:



        perl -pe 's/x1b[[0-9;]*[mG]//g'







        share|improve this answer














        share|improve this answer



        share|improve this answer








        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, whereas sed -r does NOT.
          – BVengerov
          Feb 2 '17 at 16:02














        • 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, whereas sed -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










        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'





        share|improve this answer



























          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'





          share|improve this answer

























            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'





            share|improve this answer














            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'






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 21 '12 at 3:27









            user001

            87541427




            87541427










            answered Jan 21 '12 at 1:39









            grawity

            230k35483542




            230k35483542






















                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'





                share|improve this answer























                • 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




                  @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















                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'





                share|improve this answer























                • 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




                  @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













                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'





                share|improve this answer














                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'






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Feb 11 '16 at 9:08

























                answered Jul 4 '14 at 12:44









                Sindre Sorhus

                2741215




                2741215












                • 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




                  @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


















                • 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




                  @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
















                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










                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.






                share|improve this answer



















                • 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















                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.






                share|improve this answer



















                • 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













                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.






                share|improve this answer














                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.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 13 '13 at 22:14

























                answered Mar 13 '13 at 22:08









                castl3bravo

                212




                212








                • 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














                • 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








                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










                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"





                share|improve this answer

























                  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"





                  share|improve this answer























                    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"





                    share|improve this answer












                    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"






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Apr 26 '17 at 7:40









                    Tom Hale

                    794721




                    794721






















                        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'





                        share|improve this answer

















                        • 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

















                        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'





                        share|improve this answer

















                        • 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















                        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'





                        share|improve this answer












                        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'






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Jul 3 '13 at 13:45









                        Michał Faleński

                        91




                        91








                        • 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
















                        • 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










                        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












                        up vote
                        0
                        down vote













                        This is what worked for me (tested on Mac OS X)



                        perl -pe 's/[[0-9;]*[mGKF]//g'





                        share|improve this answer

























                          up vote
                          0
                          down vote













                          This is what worked for me (tested on Mac OS X)



                          perl -pe 's/[[0-9;]*[mGKF]//g'





                          share|improve this answer























                            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'





                            share|improve this answer












                            This is what worked for me (tested on Mac OS X)



                            perl -pe 's/[[0-9;]*[mGKF]//g'






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Sep 16 '17 at 0:01









                            Miguel Mota

                            1012




                            1012






























                                draft saved

                                draft discarded




















































                                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.




                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                Сан-Квентин

                                Алькесар

                                Josef Freinademetz