正規表現「.*word1.*word2.*」のマニュアルページグローバル検索では、これら2つの単語を含むページは見つかりません。

正規表現「.*word1.*word2.*」のマニュアルページグローバル検索では、これら2つの単語を含むページは見つかりません。

manもっと効率的に使いたいです。私はそれを試してみることにしました--regex。しかし:

~$ man --regex -K '.*textdomain.*perl.*'
--Man-- next: Locale::Messages(3pm) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]
^C
~$ man --regex -K '.*perl.*textdomain.*'
No manual entry for .*perl.*textdomain.*

私が抽出したソースコード(最初に開いたものを見つける)は空のgrep結果を生成しますが、単語(textdomain、perl)を別々に探します。

~/Documents$ grep '.*textdomain.*perl.*' Locale\:\:libintlFAQ.3pm
~/Documents$ grep '.*perl.*textdomain.*' Locale\:\:libintlFAQ.3pm

perlこのファイルは(74行目など)textdomain(120行目など)の前にあります。man --regex -K '.*perl.*textdomain.*'単語が逆順になっていると、なぜ見つかりませんか?grep両方のシーケンスは1行に表示されません。実際にどのようにman --regex -K機能しますか?.*新しい道を探すべきですか?最後の答えは「システムによって異なります」と思います。https://stackoverflow.com/questions/11924480/search-in-man-page-for-words-at-the-beginning-of-line)。

ベストアンサー1

実施manman-dbほとんどのGNU / Linuxディストリビューションに見られるように、検索は次のとおりです。行ベースそしてデフォルトでは大文字と小文字を区別しない-I(/オプションを渡さない限り--match-case)。

$ man -Iw --regex -K '.*textdomain.*perl.*'
No manual entry for .*textdomain.*perl.*
$ man -w --regex -K '.*textdomain.*perl.*'
/usr/share/man/man3/Locale::libintlFAQ.3pm.gz
/usr/share/man/man3/Locale::TextDomain.3pm.gz
/usr/share/man/man3/Locale::libintlFAQ.3pm.gz
/usr/share/man/man3/Locale::TextDomain.3pm.gz
$ zgrep '.*textdomain.*perl.*' /usr/share/man/man3/Locale::libintlFAQ.3pm.gz
$ zgrep -i '.*textdomain.*perl.*' /usr/share/man/man3/Locale::libintlFAQ.3pm.gz
Locale::TextDomain::FAQ \- Frequently asked questions for libintl\-perl

perltextdomain大文字と小文字を区別するマニュアルページを検索するには:(GNUシステムで):

xargs -rd '\n' -a <(
  man -IKw perl | sort -u | xargs -rd '\n' zgrep -l textdomain --
  ) man -l --

perlman -IKwそのファイルを含むマニュアルページへのパスを返す検索を実行し、textdomainそのファイル内で検索しzgrep、その結果のパスのリストを提供しますman -l

スクリプトはman-pages-with-all-words次のように書くことができます。

#! /bin/zsh -
die() {
  print -rC1 -u2 - "$@"
  exit 1
}
(( $# )) || die "Usage: $ZSH_SCRIPT <word> [<word>...]"

typeset -U pages
pages=(
  ${(f)"$(man -IKw -- "$1")"}
)
shift
while (( $#pages && $# )); do
  pages=(
    ${(f)"$(zgrep -lF -- "$1" $pages)"}
  )
  shift
done
if (( $#pages )); then
  man -l -- $pages
else
  die "No matching man page found."
fi

shまたはPOSIX構文と同じです。

#! /bin/sh -
die() {
  [ "$#" -eq 0 ] || printf '%s\n' "$@"
  exit 1
}
[ "$#" -gt 0 ] || die "Usage: $0 <word> [<word>...]"

# tune sh split+glob: split on (sequences of) newline + no glob
IFS='
'; set -o noglob

pages=$(man -IKw -- "$1" | LC_ALL=C sort -u)
shift

while [ "$#" -gt 0 ] && [ -n "$pages" ]; do
  pages=$(
    zgrep -lF -- "$1" $pages
  )
  shift
done
if [ -n "$pages" ]; then
  man -l -- $pages
else
  die "No matching man page found."
fi

おすすめ記事