man grep | grep “color” gives no result

丶灬走出姿态 提交于 2019-12-11 01:53:16

问题


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!



来源:https://stackoverflow.com/questions/12600533/man-grep-grep-color-gives-no-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!