Reputation:
The original code was this:
curl -sL https://ftp.apnic.net/stats/apnic/delegated-apnic-latest | \
grep "apnic|JP|ipv4" | \
awk -F '|' '{ printf("%s/%d\n", $4, 32-log($5)/log(2)) }' | \
tee JP_IPv4.txt
I wanted to convert it to PowerShell, this is how far I got:
Invoke-WebRequest -Uri "https://ftp.apnic.net/stats/apnic/delegated-apnic-latest" |
Select-String -Pattern "afrinic\|ZA\|ipv6" -ca | Select-Object -exp line |
I need to figure out how to convert the last part that uses awk
command to PowerShell.
I use PowerShell 7.3
Upvotes: 1
Views: 128
Reputation: 174815
The awk
script appears to calculate the subnet prefix length from the network size.
You can replace the awk log(...)
function call with [Math]::Log(...)
in PowerShell to perform the exact same calculation:
# this takes care of the `curl` part
Invoke-WebRequest -Uri "https://ftp.apnic.net/stats/apnic/delegated-apnic-latest" -OutFile delegated_networks.txt
# this takes care of the `grep` part
Get-Content delegated_networks.txt |Where-Object {$_ -like 'apnic|JP|ipv4|*'}
# this emulates the `awk | tee` part
$data|ForEach-Object {
# -F '|'
$cells = $_.Split('|')
# '{ printf("%s/%d\n", $4, 32-log($5)/log(2)) }'
'{0}/{1}' -f $cells[3],(32 - [Math]::Log($cells[4])/[Math]::Log(2))
} |Tee-Object -FilePath JP_ipv4.txt
As @mklement0 kindly points out, you can also combine the log-conversion operation into a single call in PowerShell:
PS ~>[Math]::Log(4096)/[Math]::Log(2)
12
PS ~>[Math]::Log(4096, 2)
12
Upvotes: 2