How to strip color codes out of stdout and pipe to file and stdout

21,343

According to removing colors from output, you command should be:

printf "\n$(tput setaf 6)| $(tput sgr0)$(tput setaf 7)Sourcing files...\033[m\n" |\
sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" |tee install.log
w/o "-r"
sed "s/\x1B\[\([0-9]\{1,2\}\(;[0-9]\{1,2\}\)\?\)\?[mGK]//g"

For convenience reasons you could also create an alias in /etc/profile

alias stripcolors='sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g"'
w/o '-r'
alias stripcolors='sed "s/\x1B\[\([0-9]\{1,2\}\(;[0-9]\{1,2\}\)\?\)\?[mGK]//g"'

[Edit]

With the given output you can check this by yourself:

#!/usr/bin/perl

while ($line=<DATA>) {
    $line =~ s/^[0-9a-f]+: //;
    while ($line =~ s/([0-9a-f]{2})(?=[0-9a-f]{2}| )//) {
      print chr(hex($1));
    }
}
__DATA__
0000000: 1b5b 316d 1b5b 3333 6de2 9aa0 2020 5761 .[1m.[33m... Wa
0000010: 726e 696e 673a 201b 2842 1b5b 6d4e 6f20 rning: .(B.[mNo
0000020: 2f55 7365 7273 2f61 7077 2f2e 6261 7368 /Users/apw/.bash
0000030: 2066 6f75 6e64 2e21 0a found.!.

The output:

$ perl checkerbunny|xxd
0000000: 1b5b 316d 1b5b 3333 6de2 9aa0 2020 5761  .[1m.[33m...  Wa
0000010: 726e 696e 673a 201b 2842 1b5b 6d4e 6f20  rning: .(B.[mNo 
0000020: 2f55 7365 7273 2f61 7077 2f2e 6261 7368  /Users/apw/.bash
0000030: 2066 6f75 6e64 2e21 0a                    found.!.

$ perl checkerbunny|stripcolors|xxd
0000000: e29a a020 2057 6172 6e69 6e67 3a20 1b28  ...  Warning: .(
0000010: 424e 6f20 2f55 7365 7273 2f61 7077 2f2e  BNo /Users/apw/.
0000020: 6261 7368 2066 6f75 6e64 2e21 0a         bash found.!.
Share:
21,343

Related videos on Youtube

iamnewton
Author by

iamnewton

I'm a very energetic and motivated, self-taught web developer with a passion for all things related to technology, specifically regarding the web, and how it can impact peoples lives. I love being able to come to work knowing that I'll have an impact on someone's life every day. Technically, I specialize in HTML/XHTML, CSS, JavaScript and Microdata for my front-end stack, while using PHP &amp; Wordpress for my CMS/backend needs. I'm obsessed with web standards, microdata, performance-driven development, and large-scale code structures/frameworks. I'm always interested in developing large-scale sites working alongside talented individuals. Other specialties include browser compliance (IE6+, Firefox 3.6+, Chrome, Opera, Safari), HTML email production, as well as media production in both audio production/recording,video production and photography. In my personal time, if I'm not catching up on my RSS feeds about technology, I'm reading books about it. I'm also really in grossed with data mining and collection. I spend a significant portion of time collecting stats and information regarding my behavior and patterns of living. I also enjoy surfing through Kickstarter, looking for some project to back or gadget to buy.

Updated on September 18, 2022

Comments

  • iamnewton
    iamnewton over 1 year

    I have a program that uses printf with some tput mixed in it and I'd like to pipe the output to stdout as well as a file. I'd prefer to use sed since I don't want any unnecessary dependencies on my script. Here's what I've got so far.

    printf "\n$(tput setaf 6)| $(tput sgr0)$(tput setaf 7)Sourcing files...\033[m\n" | tee install.log

    The only issue with this is my log file is getting all of the color output as such...

    ^[[36m| ^[(B^[[m^[[37mSourcing files...^[[m

    I'd like it to just have | Sourcing files...

    • Stéphane Chazelas
      Stéphane Chazelas over 10 years
    • iamnewton
      iamnewton over 10 years
      @StephaneChazelas, I'm trying not to use any of the perl scripts for now, and even the sed options don't seem to work.
  • iamnewton
    iamnewton over 10 years
    the -r flag doesn't appear to work on Mac BSD, so I tried using -E would seems to be the most similar and it's still outputting in the log file as such... ^[36m| ^[(B^[[m^[[37mSourcing files...^[[m It also comes back as | ^[(BSourcing files... on my Linux box.
  • Admin
    Admin over 10 years
    please show us the output of your print cmd piped with |xxd and your TERM env. var.
  • iamnewton
    iamnewton over 10 years
    TERM => xterm. 0000000: 1b5b 316d 1b5b 3333 6de2 9aa0 2020 5761 .[1m.[33m... Wa 0000010: 726e 696e 673a 201b 2842 1b5b 6d4e 6f20 rning: .(B.[mNo 0000020: 2f55 7365 7273 2f61 7077 2f2e 6261 7368 /Users/apw/.bash 0000030: 2066 6f75 6e64 2e21 0a found.!.
  • iamnewton
    iamnewton over 10 years
    I'm assuming you meant to do printf "\n$(tput setaf 6)| $(tput sgr0)$(tput setaf 7)Sourcing files...\033[m\n" | xxd?
  • Admin
    Admin over 10 years
    Could you compare the edited answer with your results.
  • iamnewton
    iamnewton over 10 years
    it still vets me the same results, except that I have to change the stripcolors function because Mac OS X doesn't understand the -r option for the sed command. I'm assuming you're on some sort of Linux distro?
  • Admin
    Admin over 10 years
    according to Extended regex there is luckily only different notation. Post is also updated. Should now be OK.
  • Jorge Bucaran
    Jorge Bucaran over 9 years
    Unfortunately, this does not work on OS X.
  • CMCDragonkai
    CMCDragonkai over 8 years
    Both sed examples don't work in busybox sed.
  • Daniel Alder
    Daniel Alder over 7 years
    I replaced the first ? by * because otherwise my ESC[00;34;47m wasn't catched
  • Rhys van der Waerden
    Rhys van der Waerden over 6 years
    This worked for me on MacOS.