rici の役立つ回答は、元のアプローチの問題をよく説明しています。
ただし、言及する価値のある別のことがあります。
man
の出力には書式制御文字が含まれており、テキスト検索の妨げになります。
検索する前に にパイプするとcol -b
、これらの制御文字が削除されます。検索結果もプレーンテキストになるという副作用に注意してください。
ただし、grep
この仕事に適したツールではありません。awk
の説明を取得するには、次のように使用することをお勧めし-O
ます。
man gcc | col -b | awk -v RS= '/^\s+-O\n/'
RS=
(空の入力レコード区切り文字) は、入力を空でない行のブロックに分割する awk のイディオムであるため、そのようなブロックの先頭でオプションを照合すると、オプションの説明を構成するすべての行が返されます。
awk
BSD/OSX などの POSIX 機能のみ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