16

I'm looking for information on the -X option of curl. However, the documentation is quite lengthy and I have to scroll down for quite a long time to get to this option. Instead, I'd like to do something like

man curl | grep -X

to get the line containing "-X". (I would also do this in conjunction with the option -A 10 to get the 10 lines after the match). However, if I try this I get

grep: option requires an argument -- 'X'
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.

Any ideas on how to use grep together with man, or more generally on how to search man pages for specific lines?

Kurt Peek
  • 52,165
  • 91
  • 301
  • 526

3 Answers3

30

You have to tell grep that -X is not an option, but the pattern to look for:

man curl | grep -- '-X'

-- indicates the end of options. Without it, grep thinks that -X is an option.

Alternatively, you can use -e to indicate that what follows is a pattern:

man curl | grep -e '-X'

If you want to see the complete man page but skip directly to the first occurrence of -X, you can use a command line option to less:

man curl | less +/-X

Typing N repeatedly then takes you to the following occurrences.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • 2
    I did `man curl | grep -a 20 -- '-X'` together with the `-a 20` option to read the first 20 lines below each result. This allowed me to quickly find the documentation on the `-X` option without having to scroll down too much. – Kurt Peek Jan 06 '17 at 11:53
  • 1
    @KurtPeek: you probably mean `-A`, because `-a` simply states that a binary files should be processed as text as well. – Willem Van Onsem Jan 06 '17 at 12:00
  • In MacOs, I want to get doc about `find` with arg `-type`, so I input `man find | grep -- '-type'`, but return this `two pseudo-types, ``local'' and ``rdonly''. The former matches find / -type f -exec echo {} \;` – Cloud Jun 02 '17 at 06:15
10

On most Linux systems, the default pager used by man is less.

If that is the case, you can search in a manpage using the / (slash) key followed by a query (here -X) and finally hit ENTER. It will highlight all cases of -X. It is of course possible that the first "hit" is not the one you want. In that case you can hit N to go to the Next hit and so browse through the entire document. In case you have jumped too far, you can use Shift+N to jump back to the previous hit.

This is not really an answer to the question how to handle this with grep, but it is simply a way to efficiently search in man.

You can read the manpage of less (man less) for further tricks on how to effectively use less to improve your experience with manpages.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
4

man will implicitly open in less if you have it installed. So maybe you could read man page for less.

less actually supports search on it's own. Just press / and write what you wanna search and enter. Use n to skip to next occurrence and N for previous.

gjask
  • 194
  • 1
  • 7
  • Be careful when stating *`man` will implicitly open in `less`*. On most systems this is the case but some Linux distros might opt for another one and furthermore a user can set the `$PAGER` or `$MANPAGER` variables and alter the pager that is used. – Willem Van Onsem Jan 05 '17 at 16:24