grep the man page of a command for hyphenated options

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.


Solution 1:

Type the below command on terminal:

man grep

Then type slash character, /, and write your search, like -i, followed by Enter. This will position the cursor at the first occurrence of the search string. Pressing n moves the cursor to the next occurrence. Pressing Shift+n moves the cursor to the previous occurrence.

Solution 2:

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
}

Solution 3:

While the simplest approach is to search with / as suggested by @girardengo, you can also use grep instead of sed which I find simpler:

$ man grep | grep -A 1 '^ *-i'
   -i, --ignore-case
          Ignore  case  distinctions  in  both  the  PATTERN and the input
          files.  (-i is specified by POSIX.)

The -A N means "Print N lines after the matching one. Just a trick to get the next few lines, similar to Avinash's sed approach.

Solution 4:

You can use the search function inside man, just pres "s", type the key you're looking for, (-i in your case) and press intro.