tags:

views:

64

answers:

3

I run

man gcc | grep "-L"

I get

Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.

How can you grep the match?

+6  A: 
man gcc | grep -- "-L"

Notice the argument "--" which means "don't treat anything that follows as an option".

Also, if you took the advice in the error message to run "grep --help" it would have shown you can also explicitly set the pattern with the -e / --regexp option.

man gcc | grep -e "-L"
man gcc | grep --regexp="-L"
Bryan Oakley
@Bryan: Thank you for your answer! It is confusing that Mac's manual for Grep does not have any documentation about the --. @ Is -- a convention in all Unix commands?
Masi
It's been a convention for unix commands for many years, but not all commands follow the convention.
Bryan Oakley
+4  A: 

As others have said, grep ( and many other gnu commands ) has a "--" option to tell grep that the remainder of the arguments are not to be treated as options to grep.

However, this will only get you lines that have "-L" on them, and that may not give you context. Are you aware that man has a built-in search capability?

   man gcc
   /-L

Then keep hitting 'n' to see the next match.

Chris Arguin
I don't think `man` has a built-in search. It's job it to format the man pages for output. Actual display is handles by a pager, typically "more" or on GNU systems "less". The / search is a function of "less"
MSalters
To give the grep search a context, the options -A, -B and -C might be used.`grep -C 3` will give 3 lines of output context in both directions.
ustun
-A looks good: man grep | grep -e '-A' -A 5
RamyenHead
+3  A: 

Another possibility:

 man gcc | grep -e "-L"
ustun