一、AWK引用外部变量的方法
1、通过"‘‘"引起变量
# a=100;awk ‘BEGIN{print "‘$a‘"/5}‘
20
2、使用-v参数
awk -v a=100 -v b=5 ‘BEGIN{print a/b}‘
二、AWK和echo计算保留小数位
a=100;awk ‘BEGIN{printf"%.3f\n","‘$a‘"/5}‘
echo "scale=4;100/5"|bc
三、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.3
变换为:
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 guangzhou
awk ‘{if($0 ~/^[a-z]/) a=$0;print $0,a}‘ aa.txt |grep -P "[0-9]"
时间: 2024-10-21 00:09:58