Awk alternative

I use the following awk command while waiting for a script to get a gateway for a specific destination

route | grep $dest | awk '{print $2}'

However, the expected script does not like $ 2 in the above statement.

Does anyone know of an alternative to awk to perform the same function as above? i.e. output 2nd column.

+5
source share
6 answers

You can use cut:

route | grep $dest | cut -d \  -f 2

This uses spaces as a field separator and extends the second field

+4
source

To answer the Pending question, single quotes do not really matter for the Tcl parser. You need to use curly braces to protect the body of the awk script:

route | grep $dest | awk {{print $2}}

awk , grep, :

route | awk -v d=$dest {$0 ~ d {print $2}}
+4

, , , . GNU Awk .

+1

SED is the best alternative to use. If you don't mind addiction, Perl should also be sufficient to solve the problem.

+1
source

Depending on the structure of your data, you can use either cut or sed to filter and print the second column.

0
source

Alternatively, you can use Perl:

perl -ne 'if(/foo/) { @_ = split(/:/); print $_[1]; }'

This will print the second token of each line containing foo, with :as a token separator.

0
source

All Articles