Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to get output of any desired description or option from a man page.

Example: I want to get the description for the autoclean option from the apt-get man page:

Please suggest a command, something like man apt-get | <somecommands> autoclean to get this output:

autoclean
           Like clean, autoclean clears out the local repository of retrieved package files. The difference is that it only removes package files that can no longer be downloaded, and are
           largely useless. This allows a cache to be maintained over a long period without it growing out of control. The configuration option APT::Clean-Installed will prevent installed
           packages from being erased if it is set to off.

I've tried man apt-get | grep autoclean but it is limited to out-put matching lines for word autoclean so I can't get full description and seems it is not that what I want!

Hence, Help me to filter part from man-page for expected output mentioned above.

(Here apt-get and autoclean is only used as example I am asking this question because when man-page is too long then filtering by such way is very helpful for getting exact thing)

share|improve this question
    
See also Can I get individual man pages for the bash builtin commands? and its duplicate for different approaches. –  Gilles 2 hours ago

1 Answer 1

up vote 7 down vote accepted

I wrote a small script to do this called he, e.g. he apt-get autoclean.

The basic strategy is: search for the word (e.g. autoclean) as the first word on a line, then print until the next blank line.

You can get something similar using basic sed, e.g.

man apt-get | sed -ne '/^  *autoclean/,/^$/p'

You can find the script on my github page (linked above) and I am including its source here:

#!/bin/sh
# he - print brief help about a single option or command
# Mikel Ward <[email protected]>
# Example Usage:
# he bash continue
# he rsync -v
scriptname=he
usage()
{
    cat <<EOF 1>&2
Usage: $scriptname <command> [<option|section>]
Example:
$scriptname bash getopts (shows documentation for bash getopts)
$scriptname ssh -v (shows documentation for ssh -v flag)
$scriptname select (shows SYNOPSIS for select(2))
$scriptname 'open(2)' (shows SYNOPSIS for open(2))
EOF
}
if test $# -lt 1; then
    usage
    exit 2
fi
manpage="$1"
# show the SYNOPSIS section if no section or option was given
option="${2:-SYNOPSIS}"
# handle manpage(number)
case $manpage in *\(*\))
    page=${manpage%\(*\)}
    section=${manpage#$page}
    section=${section#\(}
    section=${section%\)}
    manpage=$page
    ;;
esac
man ${section:+-s $section} "$manpage" | perl -n -e '
BEGIN {
    $option = "'$option'";
    $inside = 0;
    }
    if (!$inside) {
    if (/^(\s*)\Q$option\E\b/p) {
        # start of this option
        $spaces = $1;
        $inside = 1;
        print;
    }
}
else {
    if (/^$spaces\S/) {
    # start of next option;
    exit;
    }
    elsif (/^\S/) {
        # start of next section
        exit;
    }
    else {
        print;
    }
}
'
share|improve this answer
    
Also your desc script is very helpful. –  Pandya 8 hours ago
    
Just decided to rename it to he, as in short help, plus he/man. –  Mikel 8 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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