grep the man page of a command for hyphenated options

7,550

Solution 1

If you want to grep for a pattern beginning with a hyphen, use -- before the pattern you specify.

man find | grep -- -type

If you want more info, for example the entire section describing an option, you could try using Sed:

$ man find | sed -n '/-mindepth/,/^$/p'
   -mindepth levels
          Do  not apply any tests or actions at levels less than levels (a
          non-negative integer).  -mindepth  1  means  process  all  files
          except the command line arguments.

However, this won't work for every option you might search for. For example:

$ man find | sed -n '/^[[:space:]]*-type/,/^$/p'
   -type c
          File is of type c:

Not very helpful. Worse, for some options you could be misled into thinking you'd read the whole text about the option when you really hadn't. For example, searching -delete omits the very important WARNING contained as a second paragraph under that heading.


My recommendation is to use a standard call to man with the LESS environment variable set. I use it quite commonly in my answers on this site.

LESS='+/^[[:space:]]*-type' man find

To learn more about how this works, see:

LESS='+/^[[:space:]]*LESS ' man less
LESS='+/\+cmd' man less
LESS='+/\/' man less

If you just want to find the option quickly and interactively in the man page, learn to use less's search capabilities. And also see:

Solution 2

Or pipe to less and feed that a search term:

man 1 find | less -p ' -type'

(This may fail depending on exactly what less is feed, e.g. if -type has been bolded up with backspaces.)

Share:
7,550

Related videos on Youtube

karel
Author by

karel

Updated on September 18, 2022

Comments

  • karel
    karel over 1 year

    When I grep the man page of the find command for matches to type it returns a lot of search results that I don't want. Instead I want to use a command that returns only the search results for -type.

    The command man find | grep -type doesn't work. It returns:

    grep: invalid option -- 't'
    
  • karel
    karel over 7 years
    Thank you for posting. The commands in your answer returned the results that I was looking for. I will accept an answer after a day or two, so please be patient.
  • Wildcard
    Wildcard over 7 years
    @karel, no problem, I'm very patient. :) A little puzzled, though: I know you can't start a bounty on a question for two days, but I believe the time limit before you can accept an answer is only 15 minutes or so.
  • Sundeep
    Sundeep over 7 years
    man find | sed -n '/-type/,/^$/p' gives lot more than what you posted as it will match -type anywhere in the line... am working on small script myself to search man or help (for builtin) and currently using awk which still has few quirks to solve.. awk -v RS= -v rx="^\\\s*$arg\\\>" '$0 ~ rx' "$file" where arg would be -type in this case
  • Wildcard
    Wildcard over 7 years
    @Sundeep, true. I updated with the actual command used. I didn't think it crucial because my point is that's not a good way to get the data, but it's better this way; thanks.
  • Wildcard
    Wildcard over 7 years
    BTW, @Sundeep, you might want to try parsing the underlying troff files containing the original man page info with format information, instead of the text output of the man command.
  • Sundeep
    Sundeep over 7 years
    @Wildcard will check it out, thanks... irregular spacing between different man pages makes it lot tougher and require patches.. my script so far works for most of my use cases though, needed a script to work a bit like explainshell, for ex: ch ls -latr -h
  • Wildcard
    Wildcard over 7 years
    @Sundeep, here's the starting point for you. (Be sure to follow the link in that answer.) There's a LOT to know about troff. Ping me in chat when you finish diving down the rabbit hole. :)
  • Wildcard
    Wildcard over 7 years
    Pipe it through col -bx first.
  • Sundeep
    Sundeep over 7 years
    @Wildcard, here's my current version of script .. as you've pointed out in this answer, it has its drawbacks but works well for many cases... will check out the groff rabbit hole and get back to you :)