Skip to content

Instantly share code, notes, and snippets.

@jpluscplusm
Created February 5, 2020 22:32
Show Gist options
  • Save jpluscplusm/43eb911d9ed853a6a03e3a269cdc0937 to your computer and use it in GitHub Desktop.
Save jpluscplusm/43eb911d9ed853a6a03e3a269cdc0937 to your computer and use it in GitHub Desktop.
AWK IPv4 to/from decimal
#!/usr/bin/awk
# 1-liner: awk '{for(i=0;i<4;i++){byte=$1%256;ip="." byte ip;$1-=byte;$1/=256}}END{sub(".","",ip);print ip}'
{
for(i=0; i<4; i++) {
byte = $1 % 256
ip = "." byte ip
$1 -= byte
$1 /= 256
}
}
END{
sub(".","",ip)
print ip
}
#!/usr/bin/awk
# 1-liner: awk 'BEGIN{RS="."}{dec*=256;dec+=$1}END{print dec}'
BEGIN{
RS="."
}
{
dec*=256
dec+=$1
}
END{
print dec
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment