3

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
  • You said you don't want an sed or awk solution, but what about one done using head? – aliceinpalth Feb 8 '18 at 21:02
  • @aliceinpalth well thats kind of limited in some sort , just like using tails, I prefer grep, but feel free to share your solution, I bet someone else can enrich their knowledge – DRP Feb 8 '18 at 21:11
5

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.

  • Works. Do you mind explaining me the knobs please, I dont want to just copy/paste without understanding. – DRP Feb 8 '18 at 21:09
  • 1
    Done and done; if you need more detail, let me know. – DopeGhoti Feb 8 '18 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.