如何在手册页中找到特定的开关
假设我想在不滚动的情况下知道grep
命令中-i
开关的用法。 我只需要该命令的规范或至少看到屏幕显示第一个。 又怎样? 正如你可以说的那样,一般不仅仅是为了grep -i
。
试试这个简单的sed
命令,
$ 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.)
说明:
sed -n '/-i, --ignore-case/,+2p' |<-Search pattern->|
它将打印包含搜索模式的行以及在搜索模式行下方的2行。
要么
您只需在搜索模板中仅提供标记,如下所示。
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.
您可以将此脚本添加到.bashrc
( $HOME/.bashrc
)以便快速访问:
mangrep(){ USAGE="mangrep " if [[ "$#" -ne "2" ]] then echo "Usage: $USAGE" else man "$1" | sed -n "/ *"$2", -/,+3p" fi }
在终端上键入以下命令:
man grep
然后键入斜杠字符/ ,并编写搜索,如-i
,然后按Enter键 。 这会将光标定位在第一次出现的搜索字符串中。 按n将光标移动到下一个出现位置。 按Shift + n将光标移动到上一个出现位置。
虽然最简单的方法是使用@girardengo的建议进行搜索,但你也可以使用grep
而不是sed
,我觉得更简单:
$ 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.)
-AN
表示“在匹配之后打印N行。只需要获得接下来几行的技巧,类似于Avinash的 sed
方法。
您可以在man
里面使用搜索function,只需要输入"s"
,输入您要查找的键,(在您的情况下为-i)并按下介绍。
我所知道的最有效的方法是在man页面中搜索-i
(这个站点似乎无法呈现我的代码。我的意思是
)。 这是3个空格(您可能需要更多/更少的空间),然后是您正在寻找的旗帜。 它几乎总是在我的经验中起作用,并且在它不起作用的情况下你可以改变它的某些变体。
它的工作原理是因为标志的实际文档通常是缩进的。 它避免在其他部分找到其他提及的旗帜,因为它们之前通常只有一个空格。
通过所有答案可能没问题,但我认为你只关注一篇文档,而不是全部。 例如,要查找grep文档的-i
开关:
info grep Invoking Command-line\ Options Matching\ Control
我将找到有关“grep”的所有信息,如何“调用”“匹配控件”的特定“命令行选项”。 可悲的是,它并没有比这更深刻,但它在第一,25行中有-i
, -y
,– --ignore-case
,这是合理的,你不必一直向下滚动。
此解决方案更灵活,还允许您搜索所有信息:
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
(必须使用--ignore-case
而不是-i
因为它太常见了,但你可以在任何情况下只处理输出信息)
在这种情况下,您同时拥有信息页面的名称和确切的部分。 啊,差点忘了,你也可以通过信息页面的大部分标签 。
您可以使用Perl及其“段落模式”仅提取相关段落:
man grep | perl -00 -ne 'print if / -i/'
我最喜欢在man
页中查找给定选项的方法是使用像\s{2,}-option
这样的正则表达式。 对于手头的问题,你可以做man grep
然后输入正则表达式:
/\s{3,}-i
要么
/ {3,}-i ^------------- space here
只有当后跟三个或更多空格时才会匹配-i
。
由于默认情况下使用less
作为man
的寻呼机,因此您可以使用LESS
环境变量传递模式以在less
打开页面时进行搜索。 这与例如man ls
相同,然后通过键入/然后输入模式(例如--all
或-a
来搜索选项--all
或-a
。
所有这些都可以通过:
LESS='+/--all' man ls
要么
LESS='+/-a' man ls
输入/
后要搜索的内容。
据推测,与短期选项(例如-a
)相比,这对于长选项(例如--all
)最有效。