理解grep的行为

时间:2015-12-09 06:06:30

标签: linux macos bash

我在mac os X下,我想在Homebrew的文档中找到一些单词。

这是文档的一部分(man brew): enter image description here

我试过这个命令

man brew | grep -e 'another installed' --color -B 2 

并且效果很好,低于结果:

   o   leaves: Show  installed  formulae  that  are  not  dependencies  of
       another installed formula.

此命令

man brew | grep -e 'leaves' --color -B 2 

反而没有产生任何输出。 所以我试着用

brew | grep -e 'leaves.*' --color -B 2

但它也没有用。

我想用grep找到“离开”这个词。在Homebrew的文档中,打印最近的行,以便能够理解这个选项的含义。 我知道这可能是一个愚蠢的错误,但我无法弄清楚它在哪里。 有人能帮助我吗?

1 个答案:

答案 0 :(得分:1)

grep问题不在于man。当您查看手册页时,格式代码会散布,以使文本显示为粗体(或带下划线等)。

bash-3.2@yosemite$ man brew | grep l..e..a..v..e..s
       o   leaves: Show  installed  formulae  that  are  not  dependencies  of

bash-3.2@yosemite$ man brew | grep l..e..a..v..e..s | xxd
0000000: 2020 2020 2020 202b 086f 2020 206c 086c         +.o   l.l
0000010: 6508 6561 0861 7608 7665 0865 7308 733a  e.ea.av.ve.es.s:
0000020: 2053 686f 7720 2069 6e73 7461 6c6c 6564   Show  installed
0000030: 2020 666f 726d 756c 6165 2020 7468 6174    formulae  that
0000040: 2020 6172 6520 206e 6f74 2020 6465 7065    are  not  depe
0000050: 6e64 656e 6369 6573 2020 6f66 0a         ndencies  of.

传统的解决方法是通过colcrt过滤格式化的输出:

bash-3.2@yosemite$ man brew | colcrt | grep leaves
       +   leaves: Show  installed  formulae  that  are  not  dependencies  of

...但是生成的文本只是粗略地强制为ASCII。对大多数人来说,更好的解决方案是使用LESS作为手动寻呼机。

bash-3.2@yosemite$ export PAGER=less
bash-3.2@yosemite$ man brew
# ... type /leaves at the `less` prompt
   o   leaves: Show  installed  formulae  that  are  not  dependencies  of
       another installed formula.