Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
3  
learn searching in Vim –  arsaKasra May 13 at 22:31

7 Answers 7

up vote 10 down vote accepted

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
}
share|improve this answer
    
I added a bashrc snippet and syntax highlighting to your post. I hope you don't mind ☺. –  Glutanimate May 11 at 14:00

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.

share|improve this answer
1  
It will locate all the -i's the man grep page.But OP wants only the description related to -i flag in man page. –  Avinash Raj May 11 at 12:57
2  
@AvinashRaj no, it will allow you to cycle through each occurrence of -i. That's exactly what the OP wants. –  terdon May 11 at 13:02

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.

share|improve this answer

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.

share|improve this answer
    
I type man grep after that I press s after that I see log file: after that I type -i and ENTER but I do not see the description Or there is somthing which I do not know –  lion May 11 at 12:27

The most efficient method I am aware of is to search the man page for -i (This site seems to fail to render my code. What I mean is <space><space><space>-i). That's 3 spaces (you may need more/less spaces) followed by the flag you're looking for. It almost always works in my experience, and you can change to some variant of it in cases where it doesn't work.

It works because the actual documentation of the flags are typically indented. It avoids finding other mentions of the flag in other sections, because there's usually only one space before them.

share|improve this answer

Or, you can let this site do the searching for you:

http://explainshell.com/explain?cmd=grep+-i

You have to switch from using the terminal to a browser for a bit, but there's ways around that too.

share|improve this answer

Through all answers may be fine, but I think you are focusing in only a piece of documentation, not all. For example, to find the -i switch of the grep documentation:

info grep Invoking Command-line\ Options Matching\ Control

I will find all the information about "grep", how to "invoke" the specific "command-line options" for "matching control". Sadly it doesn't go more deeper than that, but it has -i, -y, --ignore-case in the firsts 25 lines, something reasonable that you don't have to scroll all your way down.

This solution is the more flexible and also allows you to search all the infopages:

info --apropos=--ignore-case
"(coreutils)join invocation" -- --ignore-case <1>
"(coreutils)uniq invocation" -- --ignore-case <2>
"(coreutils)sort invocation" -- --ignore-case
"(gettext)msggrep Invocation" -- --ignore-case, ‘msggrep’ option
"(grep)Matching Control" -- --ignore-case

(had to use --ignore-case instead of -i since it was too common, but you can just process the output to info in any case)

In this case, you have both the name of the info page and the exact section. Ah, almost forgot, you can also just tab your way through most section of the info pages.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.