2017-02-27 45 views
0

我確實使用grep很多,但我想提高一點。什麼是最簡單的方法來grep'男人grep'的標誌

關於這個問題。我想縮小man條目以找出什麼在grep -v 'pattern' filename-v主張的解釋,主要是這樣的:

-v, --invert-match 
     Selected lines are those not matching any of the specified patterns. 

因此,要找到行之後的下一個五行包含-v我想:

man grep | grep -A 5 -v

man grep | grep -A 5 '-v'

但他們返回:

usage: grep [-abcDEFGHhIiJLlmnOoqRSsUVvwxZ] [-A num] [-B num] [-C[num]] 
    [-e pattern] [-f file] [--binary-files=value] [--color=when] 
    [--context[=num]] [--directories=action] [--label] [--line-buffered] 
    [--null] [pattern] [file ...] 

這讓我困惑,因爲:

man grep | grep -A 5 'Selected'

man grep | grep -A 5 Selected

做的工作。

我的方法有什麼問題?有沒有更簡單的方法來實現我所需要的?

+1

有關使用通過它瀏覽'/'其次是你正在尋找的術語是什麼? – fedorqui

+3

我同意上面的搜索形式,但是,您的問題是您的搜索項以連字符開頭,因此grep認爲它是另一種選擇。像許多命令一樣,你可以在正則表達式之前使用2個連字符( - ),這會告訴命令將下一個內容看作文本。你也可以簡單地將連字符轉義(\ - ) – grail

+1

這個Q不是關於爲StackOverflow定義的編程。它可能更適合於S.E.相關網站http://superuser.com或http://unix.stackexchange.com(Unix和Linux)。使用Q底部的'flag'鏈接並請主持人移動它。請不要在2個不同的網站上發佈相同的Q.在此處發佈更多Q​​值之前,請閱讀http://stackoverflow.com/help/how-to-ask http://stackoverflow.com/help/dont-ask和http://stackoverflow.com/help/mcve。祝你好運。 – shellter

回答

0

一種方法是直接解析命令的Info文檔。如果運行info grep(或其他命令),則通常會找到更詳細和更好的結構化文檔,從而使您可以精確定位所需的部分。

以下是將打印出相關Info節的選項/變/等功能:

info_search() { 
    info --subnodes "$1" -o - 2>&- \ 
    | awk -v RS='' "/(^|\n)(‘|'|\`)$2((,|\[|).*)?(’|')\n/" 
} 

這應該在Linux/MacOS的/ BSD工作。輸出是這樣的:

 
$ info_search grep -v 
‘-v’ 
‘--invert-match’ 
    Invert the sense of matching, to select non-matching lines. (‘-v’ 
    is specified by POSIX.) 
 
$ info_search gawk RS 
'RS == "\n"' 
    Records are separated by the newline character ('\n'). In effect, 
    every line in the data file is a separate record, including blank 
... 
 
$ info_search bash -i 
`-i' 
    Force the shell to run interactively. Interactive shells are 
... 
相關問題