Skip to content

Instantly share code, notes, and snippets.

@SOF3
Created July 3, 2024 11:14
Show Gist options
  • Save SOF3/ae2eaac0b6566f92ace3060daee94472 to your computer and use it in GitHub Desktop.
Save SOF3/ae2eaac0b6566f92ace3060daee94472 to your computer and use it in GitHub Desktop.
Parse k8s quantities with jq
def parse_quantity:
{n: -3, u: -2, m: -1, k: 1, M: 2, G: 3} as $units |
if .[-1:] == "i" then
{base: 1024, str: .[:-1]}
else
{base: 1000, str: .}
fi |
.str[-1:] as $unit |
if $units | has($unit) then
pow(10; $units[$unit]) * (.str[:-1] | tonumber)
elif .base == 1000 then
tonumber
else
error("unknown unit in \(.str\)")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment