Ubuntu – grep the man page of a command for hyphenated options

command linemanpage

Suppose I want to know the usage of -i switch in grep command without scrolling. I need the specification just for that command or at least see the screen show that first. So how? As you can say that in general not just for grep -i.

Best Answer

Try this simple sed command,

$ man grep | sed -n '/-i, --ignore-case/,+2p'
    -i, --ignore-case
              Ignore  case  distinctions  in  both  the  PATTERN and the input
              files.  (-i is specified by POSIX.)

Explanation:

sed -n '/-i, --ignore-case/,+2p'

        |<-Search pattern->|

It will print the line which contains the search pattern along with 2 lines which present just below to the search pattern line.

OR

You can simply give only the flags in the search patten like below.

avinash@avinash-Lenovo-IdeaPad-Z500:~$ man grep | sed -n '/ *i, -/,+3p'
       -i, --ignore-case
              Ignore  case  distinctions  in  both  the  PATTERN and the input
              files.  (-i is specified by POSIX.)

avinash@avinash-Lenovo-IdeaPad-Z500:~$ man grep | sed -n '/ *V, -/,+3p'
       -V, --version
              Print  the version number of grep to the standard output stream.
              This version number should be included in all bug  reports  (see
              below).
avinash@avinash-Lenovo-IdeaPad-Z500:~$ man grep | sed -n '/ *F, -/,+3p'
       -F, --fixed-strings
              Interpret PATTERN as a  list  of  fixed  strings,  separated  by
              newlines,  any  of  which is to be matched.  (-F is specified by
              POSIX.)
avinash@avinash-Lenovo-IdeaPad-Z500:~$ man grep | sed -n '/ *G, -/,+3p'
       -G, --basic-regexp
              Interpret PATTERN  as  a  basic  regular  expression  (BRE,  see
              below).  This is the default.

You can add this script to your .bashrc ($HOME/.bashrc) for quick access:

mangrep(){
    USAGE="mangrep <application> <switch>"
    if [[ "$#" -ne "2" ]]
      then
          echo "Usage: $USAGE"
      else
          man "$1" | sed -n "/ *"$2", -/,+3p"
    fi
}
Related Question