grep --help コンテンツメソッド

grep --help コンテンツメソッド

ルーティングコマンドでヘルプスイッチ(--help)を使用すると、次の出力が生成されます。

root@theapprentice:~# route --help 
Usage: route [-nNvee] [-FC] [<AF>]           List kernel routing tables
       route [-v] [-FC] {add|del|flush} ...  Modify routing table for AF.

       route {-h|--help} [<AF>]              Detailed usage syntax for specified AF.
       route {-V|--version}                  Display version/author and exit.

        -v, --verbose            be verbose
        -n, --numeric            don't resolve names
        -e, --extend             display other/more information
        -F, --fib                display Forwarding Information Base (default)
        -C, --cache              display routing cache instead of FIB

  <AF>=Use -4, -6, '-A <af>' or '--<af>'; default: inet
  List of possible address families (which support routing):
    inet (DARPA Internet) inet6 (IPv6) ax25 (AMPR AX.25) 
    netrom (AMPR NET/ROM) ipx (Novell IPX) ddp (Appletalk DDP) 
    x25 (CCITT X.25) 

「add」という単語に基づいて2行目だけを検索したいと思います。

route [-v] [-FC] {add|del|flush} ...  Modify routing table for AF.

sedやawkは必要ありません。

私は以下を試してみました。

route --help |grep add
route --help |grep -o add
route --help |grep -E add
route --help |grep -E -o add
route --help |grep -E -o "add"
route --help |grep -E -o {add|del|flush} 
route --help |grep -w {add|del|flush}  <<<this one did not even work

ベストアンサー1

の出力はroute --help標準出力ではなく標準エラーに書き込まれます。その出力の場合は、grep標準出力にリダイレクトする必要があります。

$ route --help 2>&1 | grep -m1 'add'
       route [-v] [-FC] {add|del|flush} ...  Modify routing table for AF.

この構文は2>&1シェルに次のように言います。「標準エラーに記録された内容を標準出力に記録します。」意味) ))) 停止する-m1よう指示します。grep一度一致するものを検索し、最後の行から 3 行目を避けてください。

おすすめ記事