Is there a way to find the flag on the manual page?

I am trying to find a way to find a specific flag on a man page. Usually I type '/' to search for something, and then something like "-Werror" to find a specific flag. The fact is that there are man pages (gcc is the one that motivates me right now) that have many links to flags in their text, so there are a lot of entries.

This is not such a big deal, but perhaps it can be done a little better. I thought about searching for something like "-O \ n", but it didn’t work (maybe because the human program doesn’t use C-screens?) Then I tried something like man gcc | grep $'-O\n'since I remind you that the line with one quotation mark preceded by a dollar sign, bash can interpret common C-screens ... This did not work, grep repeated the entire man page.

What brought me here: why? or rather, can this be done?

+8
source share
6 answers

rici's helpful answer well explains the problem with the original approach.

However, another thing worth mentioning:

man , .

col -b , - , , .

, grep ; awk , -O:

man gcc | col -b | awk -v RS= '/^\s+-O\n/'
  • RS= ( ) - awk, , , , .

POSIX- -O awk 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 function manopt() - ~/.bashrc, :

# SYNOPSIS
#   manopt command opt
#
# DESCRIPTION
#   Returns the portion of COMMAND 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():

.

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
+6

, grep $'-O\n', , grep.

grep , -, . - grep -- -O$, , grep -e -O$, . , grep , , $'foo\n' - , foo , .

, -e, , , grep .

GNU, gcc, info, , . info gcc , . Linux , GNU/linux, info, man . , debian/ubuntu, gcc info, gcc-doc. ( -doc .)

gcc :

info gcc "option index" O

info gcc --index-search=funroll-loops

, -O:

info -O gawk
+3

, "" , "", man- :

/pattern
    Search  forward  in  the file for the N-th line containing the pattern.
    N defaults to 1.  The pattern is a regular expression, as recognized by the
    regular expression library supplied by your system.  The search starts at the
    first line displayed (but see the -a and -j options, which change this).

, "-O $" man, , . , , .

grep $'- O\n' .

+1
man gcc | grep "\-" 

, .

: , , , .

0

manbased on an environment variable ( EDITORif I'm not mistaken). You can change this value from more(the default value), for example, to emacs, and then, when used manon your system, a session opens emacswhere you can search and view it as you wish.

0
source

I use the following:

man some_command | col -b | grep -A5 -- 'your_request'

Examples:

  • man man | col -b | grep -A5 -- '-K'

  • man grep | col -b | grep -A5 -- '-e patt'

You can make an alias for this.

0
source

All Articles