rici 的有用答案很好地解释了原始方法的问题。
不过,还有一点值得一提:
man
的输出包含格式控制字符,这会干扰文本搜索。
如果您在 search之前使用管道传输到col -b
,这些控制字符将被删除 - 请注意搜索结果也将是纯文本的副作用。
但是,grep
这不是这项工作的正确工具;我建议使用awk
如下方式获取描述-O
:
man gcc | col -b | awk -v RS= '/^\s+-O\n/'
RS=
(一个空的输入记录分隔符)是一个 awk 习惯用法,它将输入分成非空行的块,因此在此类块的开头匹配选项可确保返回包含选项描述的所有行。
如果你有一个 POSIX-features-onlyawk
例如 BSD/OSX awk
,使用这个版本:
man gcc | col -b | awk -v RS= '/^[[:blank:]]+-O\n/'
显然,这样的命令键入起来有些麻烦,所以在下面找到泛型bash
函数,它manopt
从其man
页面返回指定命令的指定选项的描述。(可能存在误报和误报,但总体而言效果很好。)
例子:
manopt gcc O # search `man gcc` for description of `-O`
manopt grep regexp # search `man grep` for description of `--regexp`
manopt find '-exec.*' # search `man find` for all actions _starting with_ '-exec'
bash
功能manopt()
- 放置在 中~/.bashrc
,例如:
# SYNOPSIS
# manopt command opt
#
# DESCRIPTION
# Returns the portion of COMMAND's man page describing option OPT.
# Note: Result is plain text - formatting is lost.
#
# OPT may be a short option (e.g., -F) or long option (e.g., --fixed-strings);
# specifying the preceding '-' or '--' is OPTIONAL - UNLESS with long option
# names preceded only by *1* '-', such as the actions for the `find` command.
#
# Matching is exact by default; to turn on prefix matching for long options,
# quote the prefix and append '.*', e.g.: `manopt find '-exec.*'` finds
# both '-exec' and 'execdir'.
#
# EXAMPLES
# manopt ls l # same as: manopt ls -l
# manopt sort reverse # same as: manopt sort --reverse
# manopt find -print # MUST prefix with '-' here.
# manopt find '-exec.*' # find options *starting* with '-exec'
manopt() {
local cmd=$1 opt=$2
[[ $opt == -* ]] || { (( ${#opt} == 1 )) && opt="-$opt" || opt="--$opt"; }
man "$cmd" | col -b | awk -v opt="$opt" -v RS= '$0 ~ "(^|,)[[:blank:]]+" opt "([[:punct:][:space:]]|$)"'
}
fish
实施manopt()
:
由Ivan Aracki提供。
function manopt
set -l cmd $argv[1]
set -l opt $argv[2]
if not echo $opt | grep '^-' >/dev/null
if [ (string length $opt) = 1 ]
set opt "-$opt"
else
set opt "--$opt"
end
end
man "$cmd" | col -b | awk -v opt="$opt" -v RS= '$0 ~ "(^|,)[[:blank:]]+" opt "([[:punct:][:space:]]|$)"'
end