Full-text search for manpages and get the list of the name and descriptions from a console like apropos(1)

Is there any way to do a full-text search for manpages and get the list of the name and descriptions of relevant manpages from a console like apropos(1)?

You can full-text search the content of manpages with man -K. But there are three problems:

  1. man -K does not show the title of the first result to console.

  2. man -K shows only the title of manpages like this:

    --Man-- next: ansible(1) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]

  3. man -K requires Ctrl-D to skip viewing the content of manpages.
    So you cannot use yes(1) to pass the response to man -K.

here is solutions:

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Solution 1

There’s probably an easier way, but I find man -K can be combined with -w to print the paths of the files that contain the search terms, and you could combine that with lexgrog for instance to extract the whatis info:

$ man -Kws1 apropos | sort -u | xargs -rd '\n' lexgrog | perl -pe's/.*?: "(.*)"/$1/'
apropos - search the manual page names and descriptions
emacs - GNU project Emacs editor
groffer - display groff files and man pages on X and tty
info - read Info documents
lexgrog - parse header information in man pages
man - an interface to the system reference manuals
manpath - determine search path for manual pages
scanimage - scan an image
whatis - display one-line manual page descriptions
xman - Manual page display program for the X Window System

(here assuming GNU xargs for it’s -r and -d options, though the latter is unlikely to be necessary)

You could turn it into a full-apropos script or function such as:

#! /bin/sh -
man -Kw "[email protected]" |
  sort -u |
  xargs -rd '\n' lexgrog |
  perl -pe's/.*?: "(.*)"/$1/'

We’re missing the man section information though. Since that is in the path of the file, we could add it back with something like:

#! /bin/sh -
man -Kw "[email protected]" |
  sort -u |
  while IFS= read -r file; do
    lexgrog -- "$file" |
      perl -spe's/.*?: "(.*)"/$1/; s/ - / ($s)$&/' -- "-s=$section"

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply