14

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

12

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 3 '14 at 20:26
  • please show us the output of your print cmd piped with |xxd and your TERM env. var. – user55518 Feb 3 '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 7 '14 at 0:47
  • 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 1:54
  • 3
    Unfortunately, this does not work on OS X. – Jorge Bucaran Jan 16 '15 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.