Link to home
Start Free TrialLog in
Avatar of coder1313514512456
coder1313514512456Flag for United States of America

asked on

Apropos for commands only?

When I'm looking for commands to do an operation, I sometimes use apropos.  The problem is that apropos will list not just unix commands, but also C APIs as well.  There are plenty C APIs, and they easily outnumber commands listed.

My question:  is there a way to essentially do an apropos on commands only?

I'm not just trying to do a one line listing of commands (for example, if I wanted this for ls I'd do an "apropos '^ls('").  I am trying to get some kind of command or script that I can use that would accomplish searching the context of command descriptions as well, something I think apropos can be really good at.  Maybe I'm just forgetting or missing something really obvious here.

coder
SOLUTION
Avatar of arnold
arnold
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
apropos looks in an index of man pages, and I am not sure if that index can be rebuilt to include man pages of commands only. Look into man page of apropos and see if that is possible
Avatar of coder1313514512456

ASKER

arnold:

Essentially my point is that apropos is nice because it returns a list of descriptions and you can basically grep on those descriptions.  I still want to grep on those descriptions, but I just want results that are unix commands.  So I want any results that are from the API list.

Does that clarify my objective?
omarfarid:

Doesn't look like apropos man page really contains much of anything.  I suppose I could try to focus on rebuilding the man pages with only commands, but I'm not even sure how to begin going about to do this.  If memory serves, the man pages are interspersed with both unix command line commands and C APIs.  But I could certainly have that wrong.

what flavor of unix do you have?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
(I'm looking for a general unix solution here, I've encountered it on enough flavors to be an annoyance.  But for examples sake, let's say darwin/os x.)

The thing with grepping on section names is that there are plenty of C APIs in all the sections, it appears.  Unless there's some kind of subsection or categorization I don't know about.  And there's certainly plenty I don't know about.

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
exactly like you said:
| grep '(1)'
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ozo, gheist, omarfarid:  Although (1) and (8) do appear for commands, the problem is that they don't filter the other way, that is, they also contain C APIs and I'd be stuck with my same problem (although quite diminished, so thanks!)
arnold:  THAT looks like a really, really, good idea.  I can't try it out right now but by first blush it looks like you may just have solved my problem.  Of course!   Which it, and if it doesn't which, it's not a command.  Brilliant.
I'll get back to you geniuses.  Thanks again everybody.
 
 
 
Arnold, you are truly a talented individual.  The idea to execute a which on the first column of the apropos result list works.

I had to tweak your script just a little (you were doing a which on the column result, so it would try commands like "ls(1)" instead of "ls"), and it works perfectly.  It also shows that there are commands defined in other sections from 2 through 7, in addition to sections 1 and 8.  And this makes it really easy to also see what kinds of commands are in each section, like:   2: chmod,kill,mkdir;  3: login, logout, time;  4: ip6, route, tty;  5: crontab, passwd, uuencode;  6: banner;  7: hostname, etc.

The only thing it doesn't do (yet, anyway) is parse the apropos listing if there are commands that are listed in a comma delimited group.  Know what I mean?  So like, if apropos returns a group and then defines them, and this grouping starts with a C API but the group contains a command, the group will be ignored.

I'm going to keep this question open a little longer in case anyone can sort out the grouping problem.  I guess what you would have to do is loop as long as the apropos column you're examining ends with a comma.  Ideally only the commands of the group would be listed, but this could get complex, at least from a scripting standpoint.

I'm including the code so far.


The following "command-only apropos" works in OS X 10.5.6-9G55 (Darwin) bash 3.2.17(1), however, it lists entire apropos groups if the first in the group is a command, and ignores groups entirely if the first in the group is a C API!
 
#!/bin/bash
 
if [ -z $1 ]
        then
        echo "Usage: $0 keyword";
        exit;
fi
 
apropos $1 | while read a
        do  
        set $a
        which `echo $1 | awk '{ print substr($1, 1, index($1, "(")-1)}'` >/dev/null 2>&1
        if [ "$?" -eq "0" ]
                then
                echo "$a"
        fi  
done

Open in new window

Post a sample output of apropos that you mentioned.
apropos on systems usually returns:
ls (1) etc.

It is not that difficult to incoporate a check for a comma in the first elemet. and then run it through a for loop or split the group on a comma. splitting can be done in a perl one liner.

Sure.  Here's an example of apropos output, you can see that in Darwin/Mac OS X the parenthesis is immediately next to the command.  Is a split that difficult to do in awk?  I'd prefer awk (or sed) to perl only because it seems that perl isn't necessarily distributed with all flavors of unix and unix-like OSes.  (Though I think they certainly should be!  :) )

Also: ozo, gheist, and omarfarid:  I finally get what you are saying now.  The man pages for Linux and Solaris are organized differently than OS X and they have it so that user (1) and admin (8) commands are grouped in those sections, that makes a lot of sense and is good to know for when I'm using those systems.  And I could have sworn I used a system once that had an apropos switch option to list commands (like apropos -k or something?  An old Unix on HP or an HP/UX?).

Thanks again guys.

Example of grouping and apropos output without space near paren in OS X:
 
$ apropos 'print lines matching a pattern'
grep(1), egrep(1), fgrep(1) - print lines matching a pattern

Open in new window

It might be easier to setup a perl script to handle this type of issue or add additional logic to check the length of the response.  I have to think on what the better approach would be.
I would like to avoid perl if I can.  (I do appreciate perl!)

It would appear the awk has a split procedure that I totally forgot about.  I can't look at this right now, I'll have to revisit it in a little bit.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

sys:

In my version of OS X (now 10.5.7/9J61-Darwin 9.7.0), some examples of some (1) man pages that are not commands include stuff like bash built in commands (see builtin) like alloc, shift, source, stop, etc., and file stuff like finger.conf (which even though it is 5 will often show up on 1, again just an example),  And of course there's always commands that just aren't installed but will show in apropos (but the _which_ command does nicely filter them out via Arnold's script).

I'm still trying to get time together to work on the awk split idea.

Arnold:  Is this -very- easy in perl, or is it the case that it just can be done?  Do you prefix all perl scripts with "perl" or something?

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So Arnold what you are saying is I would have to make sure I nabbed the description with the group, then output each name with an appended description.
And the description, I suppose, would be defined as starting with two spaces and ending with $ endline?
 
 
 
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sys - apropos | egrep is close but it doesn't entirely work on my system.  But I am very satisfied with the responses I've received.
I'm going to close this.  Thanks everyone, especially Arnold!
 
Thanks everyone, you guys are great!
sys:  Let me just say one more thing here, that I totally agree with you that "perfect is the enemy of the good".  It is far too easy to gold plate code when you can just get what you need, much more efficient.
I didn't really see a way to comment to users directly (nothing in the profile areas), so that's why I'm writing this here.