2

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.

SATO Yusuke
  • 123
  • 3

1 Answers1

4

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 "$@" |
  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 "$@" |
  sort -u |
  while IFS= read -r file; do
    section=${file%/*}
    section=${section##*/man}
    lexgrog -- "$file" |
      perl -spe's/.*?: "(.*)"/$1/; s/ - / ($s)$&/' -- "-s=$section"
  done
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
  • love this! But: Also highlights a bit how awkward this all is. – Marcus Müller Jul 25 '21 at 14:41
  • @MarcusMüller, re: your now deleted answer, note that `zsh` has a `zsh/db/gdbm` module allowing you to *tie* a zsh associative array with a gdbm db. That would only give you the name => summary mapping though as I don't think mandb indexes the contents of man pages. – Stéphane Chazelas Jul 25 '21 at 15:13
  • yep, noticed that, and since I didn't really have a good solution to first fulltext-search the files, then map the filename to descriptions from the dbm, it made very little sense to keep that answer there. – Marcus Müller Jul 25 '21 at 15:16