Skip to content

Instantly share code, notes, and snippets.

@barryirwin
Created August 25, 2022 08:53
Show Gist options
  • Save barryirwin/167e4269811f0fd002d885280aa6fc33 to your computer and use it in GitHub Desktop.
Save barryirwin/167e4269811f0fd002d885280aa6fc33 to your computer and use it in GitHub Desktop.
Converting from number of IP addresses to a /notation using awk
# easy case where /24 is the smallext allocation $1 is the number
$ echo 8192 | awk '{print 32-log($1)/log(2)}'
13
$ echo 32 | awk '{print 32-log($1)/log(2)}'
5
# this can be used to convert 1.2.4.0 8192 to 1.2.3.0/19
$ echo 1.2.4.0 8192 | awk '{print $"/"32-log($2)/log(2)}'
1.2.3.0/19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment