Skip to content
Advertisement

Understanding the behavior of grep [closed]

I am under mac os X and I want to find some words in the documentation of Homebrew.

This is a part of the documentation (man brew): enter image description here

I tried this command

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

and it worked well, below the result:

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

This command

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

instead did not produce any output. So I tried to use

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

but it did not work too.

I want to use grep to find the word ‘leaves’ inside the documentation of Homebrew and print the nearest lines to be able to understand what this option mean. I know that maybe it is a stupid error but I am not able to figure out where it is. Can someone help me?

Advertisement

Answer

The problem is not with grep so much as with man. When you view a manual page, formatting codes are interspersed in order to make the text appear bold (or underlined, etc).

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.

The traditional workaround is to filter formatted output through colcrt:

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

… but the resulting text is kind of crudely forced to ASCII only. A better solution for most people is to use LESS as your manual pager.

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.
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement