1、AWK引用外部變量的方法
一、經過"''"引發變量
# a=100;awk 'BEGIN{print "'$a'"/5}'
20
二、使用-v參數
awk -v a=100 -v b=5 'BEGIN{print a/b}'
2、AWK和echo計算保留小數位
a=100;awk 'BEGIN{printf"%.3f\n","'$a'"/5}'
echo "scale=4;100/5"|bcide
3、AWK進行內容拼接
# cat aa.txt
beijing
192.168.1.1
192.168.1.2
192.168.1.3
shanghai
172.17.0.1
172.17.0.2
172.17.0.3
guangzhou
10.0.0.1
10.0.0.2
10.0.0.3it
變換爲:
192.168.1.1 beijing
192.168.1.2 beijing
192.168.1.3 beijing
172.17.0.1 shanghai
172.17.0.2 shanghai
172.17.0.3 shanghai
10.0.0.1 guangzhou
10.0.0.2 guangzhou
10.0.0.3 guangzhouclass
awk '{if($0 ~/^[a-z]/) a=$0;print $0,a}' aa.txt |grep -P "[0-9]"
awk