使用linuxjishu/13830.html target=_blank class=infotextkey>awk将子网掩码转换成前缀表示,以下是完整代码,供参考:
复制代码 代码如下:
#!/usr/bin/awk -f
{
num=gsub("255.","")
gsub(".0","")
mask=$0-0
for(i = 0;mask > 0;i++)
if (mask % 2^(7-i) == 0){
i++
break
}
else
mask -= 2^(7-i)
sum = 8*num + i
print sum
}
测试结果:
复制代码 代码如下:
[root@station1 ~]# echo "224.0.0.0"|./netmask.awk
3
[root@station1 ~]# echo "255.128.0.0"|./netmask.awk
9
[root@station1 ~]# echo "255.192.0.0"|./netmask.awk
10
[root@station1 ~]# echo "255.255.0.0"|./netmask.awk
16
[root@station1 ~]# echo "255.255.192.0"|./netmask.awk
18
[root@station1 ~]# echo "255.255.255.254"|./netmask.awk
31