Using awk with variables
x=3
A=`echo $A|awk '{print $x}'`
echo $A
doesnt print 3. How can i use 开发者_如何学编程variables with awk*
Pass variables to awk
with the -v
flag.
x=3
A=`echo $A|awk -v y=$x '{print y}'`
echo $A
You can use the variables of shell by this way: "'$your-shell-variable'"
or '$your-shell-variable'
. The former considers the variable as string, while the later considers it as number. The following is the code you want:
x=3
A=`echo $A|awk '{print "'$x'"}'`
echo $A
Uh, what's the point of echoing $A
? It just creates a useless fork and pipe.
x=3
A=`awk -v y=$x 'BEGIN {print y}'`
echo $A
And while I'm at it, this seems like a convoluted and expensive way to write A=$x
:-)
精彩评论