【问题标题】:man grep | grep "color" gives no result男人grep | grep "color" 没有结果
【发布时间】:2026-02-23 22:30:02
【问题描述】:

当我尝试使用此命令在 grep 手册中搜索颜色选项时,怎么会发生

man grep | grep "color"

我没有得到任何结果,但是当我运行时

man grep

man 输出这个 [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'.

【问题讨论】:

  • “印刷手册”是指纸上的吗?那么那个印刷手册可能不适合grep的安装版本。
  • man grep | grep color 非常适合我。
  • 这里的常规 man 命令在连接到管道时不会发送转义序列。也许你的 man 命令超载了,请确保你使用带有command man grep | grep 'color' 的实际命令。
  • @sth 我的意思是 man 输出的内容,现在在问题中进行了编辑。

标签: bash unix grep command


【解决方案1】:

尝试通过 col -b 将手册页通过管道传递,以在 grep 之前清除所有特殊字符。

例如:

man grep | col -b | grep "color"

或者,将col -b 指定为您的手册页:

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

您甚至可以在您的 shell 配置文件中设置 MANPAGER 以使其更加永久。

export MANPAGER='col -b | less'

但是,这意味着您在查看手册页时会失去漂亮的颜色。

【讨论】:

    【解决方案2】:

    “man”的输出包含转义序列(尝试man grep|cat -vT),产生单词“color”的转义序列中没有ASCII序列“color” :

       -^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'
      

      效果很好!

      【讨论】:

      • 默认设置终端为term-256color
      • 终端不显示颜色不是这里的问题。问题是 grep 在它应该显示的时候没有显示任何结果。