Preserve Colouring After Piping Grep to Grep

Preserve colouring after piping grep to grep

grep sometimes disables the color output, for example when writing to a pipe. You can override this behavior with grep --color=always

The correct command line would be

grep --color=always WORD * | grep -v AVOID

This is pretty verbose, alternatively you can just add the line

alias cgrep="grep --color=always"

to your .bashrc for example and use cgrep as the colored grep. When redefining grep you might run into trouble with scripts which rely on specific output of grep and don't like ascii escape code.

Preserve ls colouring after grep'ing

Your grep is probably removing ls's color codes because it has its own coloring turned on.

You "could" do this:

ls -l --color=always | grep --color=never pattern

However, it is very important that you understand what exactly you're grepping here. Not only is grepping ls unnecessary (use a glob instead), this particular case is grepping through not only filenames and file stats, but also through the color codes added by ls!

The real answer to your question is: Don't grep it. There is never a need to pipe ls into anything or capture its output. ls is only intended for human interpretation (eg. to look at in an interactive shell only, and for this purpose it is extremely handy, of course). As mentioned before, you can filter what files ls enumerates by using globs:

ls -l *.txt      # Show all files with filenames ending with `.txt'.
ls -l !(foo).txt # Show all files with filenames that end on `.txt' but aren't `foo.txt'. (This requires `shopt -s extglob` to be on, you can put it in ~/.bashrc)

I highly recommend you read these two excellent documents on the matter:

  • Explanation of the badness of parsing ls: http://mywiki.wooledge.org/ParsingLs
  • The power of globs: http://mywiki.wooledge.org/glob

How to retain grep color when storing in variable or piping to another command?

Use the option --color=always:

data=$(egrep -i --color=always "$search" file.data)

By default, grep does not produce color unless the output is going directly to a terminal. This is normally a good thing. The option --color=always overrides that.

For occasions when you don't want color, use --color=never.

Bash when piping colored keyword grep to file it doesn't hold the color

--color (assuming GNU grep, but probably similarly for other versions of grep supporting color) is short for --color=auto, which means the output only contains the appropriate escape sequences if grep determines its standard output is a terminal. To force color when writing to a file, use --color=always.

(What you use to view the file may or may not interpret those bytes as you expect. cat user_pretty.log will just write everything to the terminal, which will interpret them the same as if you had just run grep without redirection. If you open the file in an editor, you will probably see the editor's representation of non-printable characters.)

Preserve colors of heroku logs output when piping to other command (e.g. grep)

I have found a solution here:

script -q /dev/null heroku logs --force-colors -t -a myApp -s app | grep --color=never web.1

The color flags are no even necessary so this works as well:

script -q /dev/null heroku logs -t -a myApp -s app | grep web.1

Is it possible to colorize output piped to more?

The problem isn't that more and less aren't colourizing their output, it's that ls is not outputting the colour because it's connected to another process rather than the terminal.

You can't easily get ls to be any smarter about when it outputs colour, but you can add --color to force it to output colour when you're piping it to more

When you have colour output, use ... |less -R to make less pass the colours through to the terminal instead of showing the escape codes as text



Related Topics



Leave a reply



Submit