How to grep –help content

greppipe

Using the help switch (–help) on the route command gives the following output:

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) 

I only want to retrieve the second line based on the word "add":

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

I don't want sed or awk.

I have tried using:

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

Best Answer

The output of route --help is written to standard error; not standard output. To grep that output, you will need to redirect it to standard output:

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

The syntax 2>&1 says to the shell, "take what is written to standard error, and instead write it to standard output" (technically, it says "take what is written to file descriptor 2, and instead write it to whereever file descripter 1 is writing"). -m1 tells grep to stop searching after one match, to eschew the third-from-last line.

Related Question