47

I sometime want to pipe the color-coded output fror a process, eg. grep... but when I pipe it to another process, eg. sed, the color codes are lost...

Is the some way to keep thes codes intact ?

Here is an example which loses the colored output:

echo barney | grep barney | sed -n 1,$\ p   
Peter.O
  • 32,916
  • 4
    PS.. as general info.. I've just discovered that less can accept colorized text input ( nice :) ...eg: tree -C ~/ |less -R or ls -lR --color=always . |less -R – Peter.O Apr 11 '11 at 10:27

2 Answers2

50

Many programs that generate colored output detect if they're writing to a TTY, and switch off colors if they aren't. This is because color codes are annoying when you only want to capture the text, so they try to "do the right thing" automatically.

The simplest way to capture color output from a program like that is to tell it to write color even though it's not connected to a TTY. You'll have to read the program's documentation to find out if it has that option. (e.g., grep has the --color=always option.)

You could also use the expect script unbuffer to create a pseudo-tty like this:

echo barney | unbuffer grep barney | sed -n 1,$\ p
cjm
  • 27,160
  • 4
    thanks for that info about TTY detection and the unbuffer fake-out! – shellter Apr 07 '11 at 16:01
  • @cjm.. It's not working here (on Ubuntu 10.04 / bash 4.1.5) ...but my 10.04 repo only has a -dev version, and nothing in backports... It's a bit late here, so I'll look again tomorrow... but as you've said, these colorizing programs probably toggle according to the output destination (mhhh but how do they know it is goiong to tty..no matter) .. and they may well have an option to force it.. thanks,,, – Peter.O Apr 07 '11 at 16:25
  • @fred, they generally use isatty to find out where stdout is going. I'm not actually sure if unbuffer works when you're piping into the program as well as out of it; I don't have it installed here to try. – cjm Apr 07 '11 at 17:59
  • thanks for the unbuffer information, it help my IRC bot's output like the output in shell. – LiuYan 刘研 Nov 11 '13 at 09:05
  • @cjm How could I use unbuffer with exec so that it preserves colors in console. Command I like to modify is: exec > >(tee $LOG_FILE) 2>&1;. My question on AskUbuntu: https://askubuntu.com/q/1344347/928088. Thank you so much. – Jags Jun 09 '21 at 14:08
4

It works for me ;-! (in the current MingW environment)

echo barney | grep --color=always barney | sed -n '1,$p'
**barney**

# barney displays as red text

$ grep --version
GNU grep 2.5.4

$ sed --version
GNU sed version 4.2.1
shellter
  • 704
  • @shellter... yes, this particular grep example works for me too.. I wasn't aware of that option until you posted the answer, so thanks for that... However. I'm still wondering if there is some general way to do this.... eg tree is colorized a-la dircolors if the LS_COLORS environment variable is set and output is to tty ... Maybe(?) there is a way to trick a pipe into thinking it is outputting to a TTY.. or some such general workaround .. – Peter.O Apr 07 '11 at 14:44
  • I've just checked man tree... It too has a similar option, -C ... perhaps it is a common feature for programs that output color escape codes.. – Peter.O Apr 07 '11 at 14:47