I'm trying to find a command to search for a pattern containing hyphens inside all man pages.

I've looked at man man, and found these 3 options:

-K, --global-apropos

Search for text in all manual pages. This is a brute-force search, and is likely to take some time; if you can, you should specify a section to reduce the number of pages that need to be searched. Search terms may be simple strings (the default), or regular expressions if the --regex option is used.

-w, --where, --path, --location

Don't actually display the manual pages, but do print the location(s) of the source nroff files that would be formatted.

-S list, -s list, t--sections=list

List is a colon- or comma-separated list of order specific manual sections to search. This option overrides the $MANSECT environment variable. (The -s spelling is for compatibility with System V.)

I've tried to combine them to search for the pattern mark-modified-lines which is a readline option described in man bash:

$ man -s1 -Kw mark-modified-lines

But it doesn't find any page:

No manual entry for mark-modified-lines

And the command exits with the code 16.
I thought that maybe the syntax of the command was wrong, but it doesn't seem so, since this command correctly finds the 5 man pages on my system which contains the word guitar:

$ man -s1 -Kw guitar

  /usr/share/man/man1/ffmpeg-all.1.gz
  /usr/share/man/man1/ffserver-all.1.gz
  /usr/share/man/man1/ffplay-all.1.gz
  /usr/share/man/man1/ffmpeg-filters.1.gz
  /usr/share/man/man1/ffprobe-all.1.gz

I thought that maybe the hyphens in the word caused an issue. In man bash, I found the --regex option which is described as follows:

--regex

Show all pages with any part of either their names or their descriptions matching each page argument as a regular expression, as with apropos(1). Since there is usually no reasonable way to pick a "best" page when searching for a regular expression, this option implies -a.

I tried to use this option and replace the word mark-modified-lines with the regex mark.modified.lines, where the hyphens are themselves replaced with the metacharacter . which should match any character:

$ man -s1 -Kw --regex 'mark.modified.lines'

It still doesn't print any page, while I know that the text is written in the bash man page.

The metacharacter . in the regex seems to be parsed as expected, since this command:

$ man -s1 -Kw --regex 'mark.mo'

Prints:

  /usr/share/man/man1/x11perfcomp.1.gz
  /usr/share/man/man1/xditview.1.gz

And these 2 manpages (x11perfcomp, xditview) are both matched by the regex mark.mo. More specifically, man x11perfcomp contains this line:

Mark Moraes wrote the original scripts to compare servers.
^^^^^^^

And man xditview contains this line:

    Mark Moraes (University of Toronto)
    ^^^^^^^

However, man -s1 -Kw --regex 'mark.mo' doesn't print the bash man page:

/usr/share/man/man1/bash.1.gz

While I expected it would, since it contains this line:

mark-modified-lines (Off)
^^^^^^^

Is it possible to search for a pattern containing hyphens inside the man pages?

up vote 14 down vote accepted

man -K searches the source code of the manual pages, not their rendered form (as displayed by man). Hyphens are encoded \-, so you need to search for that:

man -s1 -Kw 'mark\-mo'

Yes, this is rather obscure. The man man page mentions, in the description of the -K option, that

Note that this searches the sources of the manual pages, not the rendered text, and so may include false positives due to things like comments in source files. Searching the rendered text would be much slower.

but using this correctly involves knowing the source representation of the text you’re searching for.

  • 1
    Obscure? Yes. Also, it's a bug. – kubanczyk Oct 22 at 19:38
  • @kubanczyk well, it matches the spec, but yes, I’d agree that the spec is buggy ;-). – Stephen Kitt Oct 22 at 20:37

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.