Advertisement
Guest User

Apropos - apropos with a capital A

a guest
Oct 7th, 2017
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.64 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ## Apropos - with a capital A
  4.  
  5. # Search all man pages for a specific term and print an apropos (1)
  6. # style output.
  7.  
  8. # ** default (non-capital) apropos command only searches
  9. # man page names and short descriptions
  10.  
  11. man -KwI "$@" |\
  12.   sed 's/.*\/\(.*\)\.[^.]*/\1/' |\
  13.   sort -u |\
  14.  
  15. while read i ; do
  16.   apropos -e -s ${i##*.} ${i%.*} |\
  17.   grep "^${i%.*} "
  18. done
  19.  
  20. # Commands explanation:
  21.  
  22. # man
  23. #  -K searches all man pages.
  24. #  -w shows the filepath to matching pages
  25. #  -I forces case sensitivity
  26. # $@ is script parameters
  27.  
  28. # sed
  29. #  .*\/ is all characters up to the last slash in filepath
  30. #  \(.*\) should match manpage-name.sectionnumber, e.g. xterm.1
  31. #  \.[^.]* matches file extension on man page internal format.
  32. #          i.e. a dot followed by non-dot characters
  33. #          = usually .gz
  34. #  \1 is the aforementioned match
  35. # *** This regexp is not robust, but works.
  36.  
  37. # sort -u
  38. #  = ditch duplicate results
  39.  
  40. # while read i
  41. #  loop through all manpage-name.sectionnumber found
  42. #  storing in variable $i
  43.  
  44. # apropos
  45. #  -e goes for exact match of text string
  46. #  -s chooses man page section number
  47.  
  48. # ${i##.} is all characters after the last dot in $i,
  49. #  i.e. this should be the section number
  50.  
  51. # ${i%.*} is all characters up to the last dot in $i,
  52. #  i.e. this should be the manpage-name
  53.  
  54. # grep
  55. #  selects only lines beginning (^) with the right manpage-name
  56. #  *** The apropos command will often return more than one result
  57. #      due to the manpage-name being mentioned in the description
  58. #      of another (likely related) manpage.
  59.  
  60. # FUTURE: Support regexps, which are blatantly ignored right now.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement