开发者

How to quote a shell variable in a TCL-expect string

I开发者_如何学JAVA'm using the following awk command in an expect script to get the gateway for a particular destination

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

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

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


You can use cut:

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

That uses spaces as the field delimiter and pulls out the second field


To answer your Expect question, single quotes have no special meaning to the Tcl parser. You need to use braces to protect the body of the awk script:

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

And as awk can do what grep does, you can get away with one less process:

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


Before switching to another utility, check if changing field separator worrks. Documentation for field separators in GNU Awk here.


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


Depending on the structure of your data, you can use either cut, or use sed to do both filtering and printing the second column.


Alternatively, you could use Perl:

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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜