can not use unix $variable in Fixed search of awk

2019-09-21 12:11发布

I can not use unix $variable in Fiexd search of awk command.

Please see below my commands.

a="NEW_TABLES NEW_INSERT"
b="NEW"
echo $a | awk -v myvar=$b -F'$0~myvar' '{print $2}'

is not returning any output

but if manually enter the $b value there , its working as below

echo $a | awk -v -F'NEW' '{print $2}'

    outputs: 
 TABLES NEW_INSERT

2条回答
何必那么认真
2楼-- · 2019-09-21 12:25

This should make it:

$ a="NEW_TABLES NEW_INSERT"

$ echo $a | awk -F"NEW_" '{print $2}'
TABLES 

$ b="NEW_"
$ echo $a | awk -F"$b" '{print $2}'
TABLES 
查看更多
家丑人穷心不美
3楼-- · 2019-09-21 12:26

Your quotings are all messed up and you can use your variable to split the line using split function:

a="NEW_TABLES NEW_INSERT"
b="NEW"
echo $a | awk -v myvar="$b" '{split($0,ary,myvar);print ary[2]}'

Outputs:

_TABLES
查看更多
登录 后发表回答