Last active
November 14, 2018 16:28
-
-
Save cybertxt/d6250c470a0329cf4466feba4450019b to your computer and use it in GitHub Desktop.
Convert between network byte ordered decimal number and dot-delimited string in AWK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/awk -f | |
BEGIN { | |
dec = ARGV[1] | |
for (e = 3; e >= 0; e--) { | |
octet = int(dec / (256 ^ e)) | |
dec -= octet * 256 ^ e | |
ip = octet delim ip | |
delim = "." | |
} | |
printf("%s\n", ip) | |
} | |
################################ | |
#!/usr/bin/awk -f | |
BEGIN { | |
ip = ARGV[1] | |
split(ip, octets, ".") | |
for (i = 1; i <= 4; i++) { | |
dec += octets[i] * 256 ** (4 - i) | |
} | |
printf("%i\n", dec) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment