[Root@lookback ~]# echo 8.8.8.8|awk-f. ' {for (i=1;i<=nf;i++) {a= '] b= $i while (b) {a=b%2 a;b=int (B/2)}printf ("%08d%s ", A,i!=nf?". ": \ n"}} '
00001000.00001000.00001000.00001000
[Root@lookback ~]#
[Root@lookback ~]#
[Root@lookback ~]# echo 8.8.8.8|awk-f. ' {printf '%d\n ', ($1* (2^24)) + ($2* (2^16)) + ($3* (2^8)) +$4} '
134744072
[Root@lookback ~]#
[Root@lookback ~]#
[Root@lookback ~]# echo 00001000.00001000.00001000.00001000|awk-f. ' Function Bin2dec (a,b,i,c) {b=length (a); C=0;for (i =1;i<=b;i++) {c+=c;if (substr (a,i,1) = = "1") C++}return c}{for (j=1;j<=nf;j++) printf ("%d%s", Bin2dec ($j), j!=nf? "." : "\ n")} '
8.8.8.8
[Root@lookback ~]#
[Root@lookback ~]#
[Root@lookback ~]# echo 1000.1000.1000.1000|awk-f. ' Function Bin2dec (a,b,i,c) {b=length (a); C=0;for (i=1;i<=b;i++) { C+=c;if (substr (a,i,1) = = "1") C++}return c}{for (j=1;j<=nf;j++) printf ("%d%s", Bin2dec ($j), j!=nf? ".": "\ n")} '
8.8.8.8
[Root@lookback ~]#
#拆分说明
Awk-f. ' {#以. do separator;
for (i=1;i<=nf;i++) {#把IP分成4段, sequentially into the loop;
A= ""; #定义一个变量a目前值是空;
b= $i; #定义一个变量b值是进入循环的值;
while (b) {#在awk里面0为假, so only b≠0 are entered into the circulation body;
A=b%2 A; #假如b =8 here a=0 "" Here "is the role of definition a above, when the next entry into the b=4 into the circulation of the body a=00, and then the next time b=2 into the circulation body a=000;
B=int (B/2) #这里就是在模拟十进制转二进制笔算那一套除以2取结果整数部分;
}
printf ("%08d%s", A,i!=nf ".": "\ n") #这里其实就是if (I!=NF) {printf "%08d", a}else{printf "%08d\n", a} of the three-head writing, to achieve the print 8-bit binary is not enough to 0;
}
}'
# # #下面是网上的awk实现十进制转二进制
# #https://www.cs.ucsb.edu/~sherwood/awk/decimal2binary.awk.txt
#!/usr/bin/gawk-f
# ___ _ _ ____ ___ ___ ____ ___ ____
# | \ | | | | | |__| |__] |___
# |__/ |__| |___ | | | | | |___
#
# The scripts were written to being usefull in
# a enviornment, but anyone is welcome
# to use them. Happy awking. -tim Sherwood
Func get01string (innum,t,retstr,i) {
Retstr = "";
T=innum;
while (t) {
if (t%2==0) {
Retstr = "0" RETSTR;
}
else{
Retstr = "1" RETSTR;
}
t = Int (T/2);
}
return retstr;
}
{
Print get01string ($);
}