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
.
-
4learn searching in Vim– arsaKasraMay 13, 2014 at 22:31
-
this might be your interest askubuntu.com/a/1281460/283843 : )– αғsнιηAug 15, 2021 at 7:44
12 Answers
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.
-
2It will locate all the
-i
's theman grep
page.But OP wants only the description related to-i
flag in man page. May 11, 2014 at 12:57 -
3@AvinashRaj no, it will allow you to cycle through each occurrence of
-i
. That's exactly what the OP wants.– terdonMay 11, 2014 at 13:02
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
}
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.
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.
-
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 May 11, 2014 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.
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.
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.
You can use Perl and its "paragraph mode" to extract only the relevant paragraph:
man grep | perl -00 -ne 'print if / -i/'
My favorite approach to find a given option in a man
page is to use a regex like \s{2,}-option
. For the question in hand, you can do man grep
and then type the regex:
/\s{3,}-i
or
/ {3,}-i
^------------- space here
This will match -i
only when followed by three or more spaces.
If you want to grep
the man <program>
results for a pattern beginning with a hyphen, use --
before the pattern you specify. --
is a flag that most programs interpret as "nothing after this should be taken as a flag". Example using man find
:
man find | grep -- -type
If you want more info, for example the entire section describing an
option, try using sed
:
$ man find | sed -n '/-mindepth/,/^$/p'
-mindepth levels
Do not apply any tests or actions at levels less than levels (a
non-negative integer). -mindepth 1 means process all files
except the command line arguments.
As less
is used by default as the pager for man
, you can use the LESS
environment variable to pass a pattern to search for when less
opens the page. This is identical to doing e.g. man ls
and then searching for the option --all
or -a
by typing / and then inputting the pattern e.g. --all
or -a
.
All these can be done by:
LESS='+/--all' man ls
Or
LESS='+/-a' man ls
Input what you want to search after /
.
Presumably this works best for the long options (e.g. --all
) compared to the short ones (e.g. -a
).
If you search for multiple options, you may use egrep:
man shutdown | egrep -- "-(r|H|h)"
if you don't want to search them one by one with /-(r|H|h) in the interactive pager less, which is possible, but better shown with a larger man page like that of ls:
man ls | egrep -- "-(a|f|h)"
man ls
# interactive
/-(a|f|h)
Filtering for leading whitespace has been shown already. Grep can be invoked to show N lines after the option with A:
man ls | egrep -- "-(a|f|h)"
man ls | egrep -A1 -- "^ *-(a|f|h)"
-a, --all
do not ignore entries starting with .
--
-f do not sort, enable -aU, disable -ls --color
--
-h, --human-readable
with -l and -s, print sizes like 1K 234M 2G etc.
With sed you may specify "from Option X to the next" but in a hurry, I didn't find out, how to suppress the first line of the next, terminating option. If you need it often, you might write a shell function, to make it easy to use:
man ls | sed -rn "/^ *-(a|f|h)/,/^ *-/p"
-a, --all
do not ignore entries starting with .
-A, --almost-all
-f do not sort, enable -aU, disable -ls --color
-F, --classify
-h, --human-readable
with -l and -s, print sizes like 1K 234M 2G etc.
--si likewise, but use powers of 1000 not 1024