18

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...

iamnewton
  • 291

2 Answers2

14

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.!.
  • 1
    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.

    – iamnewton Feb 03 '14 at 20:26
  • please show us the output of your print cmd piped with |xxd and your TERM env. var. –  Feb 03 '14 at 22:36
  • 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 Feb 07 '14 at 00:47
  • I'm assuming you meant to do printf "\n$(tput setaf 6)| $(tput sgr0)$(tput setaf 7)Sourcing files...\033[m\n" | xxd? – iamnewton Feb 07 '14 at 00:48
  • Could you compare the edited answer with your results. –  Feb 07 '14 at 11:55
  • 1
    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? – iamnewton Feb 11 '14 at 01:54
  • according to Extended regex there is luckily only different notation. Post is also updated. Should now be OK. –  Feb 11 '14 at 12:12
  • 3
    Unfortunately, this does not work on OS X. – j-- Jan 16 '15 at 13:53
  • Both sed examples don't work in busybox sed. – CMCDragonkai Feb 11 '16 at 12:17
  • I replaced the first ? by * because otherwise my ESC[00;34;47m wasn't catched – Daniel Alder Dec 13 '16 at 12:39
  • This worked for me on MacOS. – Rhys van der Waerden Oct 13 '17 at 00:10
0

Put this code:

#!/bin/bash
sed -r "s/\x1B\[([0-9]{1,3}(;[0-9]{1,2})?)?[mGK]//g"  #credits to user55518

in an executable script somewhere in your PATH and name it something like decolor.


Then you can easily use it as a color filter like this:

$ something-that-generates-color | decolor

(I noticed someone else above found a similar thing above that was called ansi2txt. I think that name is good, but it's harder to remember or find. I like having the stub 'color' in it. And too bad this isn't just a standard GNU unix/linux utility. It's very useful once you've colorized your ls, grep, tree, etc.)

Elliptical view
  • 3,921
  • 4
  • 27
  • 46