Preserve colouring after piping grep to grep

后端 未结 4 1319
遇见更好的自我
遇见更好的自我 2021-01-30 05:07

There is a simlar question in Preserve ls colouring after grep’ing but it annoys me that if you pipe colored grep output into another grep that the coloring is not preserved.

4条回答
  •  梦谈多话
    2021-01-30 05:11

    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.

提交回复
热议问题