can not use unix $variable in Fixed search of awk

2019-09-21 12:04发布

问题:

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

回答1:

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 


回答2:

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