Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

grepping man pages

by Anonymous Monk
on Aug 18, 2001 at 04:37 UTC ( [id://105851]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I want to create a script that will return only the synopis and description parts of the man page that I call with man so if I say "$0 rm" I would get a string with rm and its manpage down to the first paragraph in description. removing the SYNOPSIS and DESCRIPTION text. what are some ways I can do this? my first line...
open(MAN, "$manpath $arg |") or die"ERROR:$!\n"; @manpage = <MAN>;
help?

Replies are listed 'Best First'.
Re: grepping man pages
by guillaume (Pilgrim) on Aug 18, 2001 at 10:32 UTC
    man pages are under a format that can be read by groff. you could get the ascii text of a man page by doing this:
    open(MAN, "groff -Tascii $manpath/$manfilename |") or die"ERROR:$!\n"; @manpage = <MAN>;
    where $manpath is the path to your man pages ('/usr/local/man/' on my system).

    $manfilename is the particular man page you are trying to get.

    Look around the directory, you will understand how the files are stored.

    Hope that helps,
    Guillaume
Re: grepping man pages
by tachyon (Chancellor) on Aug 18, 2001 at 11:19 UTC

    Here is an example reading from a manpage in DATA

    $_ = join'', <DATA>; print m/^DESCRIPTION(.*?)\n\n/ms;; __DATA__ NAME perldoc - Look up Perl documentation in pod format. SYNOPSIS perldoc [-h] [-v] [-t] [-u] [-m] [-l] [-F] [-X] PageName|ModuleName|ProgramName perldoc -f BuiltinFunction perldoc -q FAQ Keyword DESCRIPTION *perldoc* looks up a piece of documentation in .pod format that is embedded in the perl installation tree or in a perl script, and displays it via `pod2man | nroff -man | $PAGER'. (In addition, if running under HP-UX, `col -x' will be used.) This is primarily used for the documentation for the perl library modules. Your system may also have man pages installed for those modules, in which case you can probably just use the man(1) command. OPTIONS -h help Prints out a brief help message.

    We get away with the one liner because in list context a regexp returns a list of what it matched so effectively we are dining a m/(this)/; print $1. We need the /m and /s modifiers. For details on why see perlman:perlre To get the entire output of the man command into a var like $_ the easiset way is to use backtics like this $_ = `man $manpage`;. Don't let just anyone supply the input value to $manpage as nasty input can do nasty things in bactics. A value like '; rm -rf *;' might be suboptimal. A short program to do this might be:

    #!/usr/bin/perl -w use strict; print "Which manpage? "; chomp ( my $manpage = <> ); # only allow say words and numbers in $manpage die "Illegal input" unless $manpage =~ m/^\w+$/; $_ = `man $manpage`; print m/^DESCRIPTION(.*?)\n\n/ms;

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://105851]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-25 09:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found