man grep | grep “color” gives no result

2019-07-19 22:40发布

问题:

How come when I try to search the grep manual for the color option with this command

man grep | grep "color"

I get no result but when I run

man grep

man outputs this [EDITED]

 --colour=[when, --color=[when]]
         Mark up the matching text with the expression stored in GREP_COLOR environment variable.  The possible values of when can be `never', `always' or `auto'.

回答1:

Try piping the man page through col -b to clean up any special characters before grepping.

For example:

man grep | col -b | grep "color"

Alternatively, specify col -b as your man pager:

man -P "col -b" grep | grep "color"

You can even set MANPAGER in your shell profile to make this more permanent.

export MANPAGER='col -b | less'

However, this means that you will lose the pretty colours when viewing man pages.



回答2:

the output of "man" contains escape sequences (try man grep|cat -vT), and the escape sequence to produce the word"color" does not have ASCII sequence "color" in it:

   -^H--^H-c^Hco^Hol^Hlo^Hou^Hur^Hr[^H[=^H=_^HW_^HH_^HE_^HN]_^H, -^H--^H-c^Hco^Hol^Hlo^Hor^Hr[^H[=^H=_^HW_^HH_^HE_^HN]
          Surround  the matching string with the marker find in G^HGR^HRE^HEP^HP_^H_C^HCO^HOL^HLO^HOR^HR
          environment variable. WHEN may be 'never', 'always', or 'auto'


回答3:

man grep | grep -E 'color'

it works perfectly!