shell传递参数给awk

发布时间:2019-08-14编辑:脚本学堂
shell传递参数给awk

shell传参数给linuxjishu/13830.html target=_blank class=infotextkey>awk
i=1
ATP-T3000:~/bhz # cat file
0f 31 00 12 00 05 01 06 03
awk "{print $$((6+i))}" file
01

awk "{print $${i}}" file
0f

只能引用数字,不能引用字母
awk "BEGIN{print "${i}"}"
1
test=10
awk "BEGIN{print "${test}"}"
10
awk "{print "${test}"}"  file
file为任意内容
10
awk "{print $test}" file
10

test1="abc"
test2="def"
echo |awk -vt1=$test1 -vt2=$test2 'BEGIN{print t1 t2}'
abcdef

test1="abc"
awk 'BEGIN{print '""$test1""'}'
abc

test=abcd
awk -va=$test '{print a}' 1
awk -va=$test 'BEGIN{print a}'
还是awk里面传shell变量的问题
sed及awk引用shell变量时可以使用双引号来实现:
在模式里面可以这样使用
b="aaa"
awk "/${b}/" filename
4 aaa 1000
3 aaa 1000
1 aaa 1000

ABC=("pcscd" "mcstransd" "dbus")
abc=("111" "222" "333")

j=0
for i in "${ABC[@]}";do
        ps -ef|grep $i|grep -v grep|awk  '{print  '${abc[$j]}',$3,$4}'
        ((j++))
done

a=20110101
awk 'BEGIN{print "adsad" > '$a'}'
awk 'BEGIN{print "adsad" > "'$a'"}'
awk  -v f=$file 'BEGIN{print "==========" > f}'

xiabao@6P9SN2X ~/test
$ cat urfile
111
222
333
444
555
a=(`cat f`)
xiabao@6P9SN2X ~/test
$ ARRAY=(`awk '{print $1}' urfile`)

xiabao@6P9SN2X ~/test
$ echo ${ARRAY[0]} ${ARRAY[1]} ${ARRAY[2]}
111 222 333