Skip to content

Instantly share code, notes, and snippets.

@SOF3
Last active December 27, 2024 05:52
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, K: 1, M: 2, G: 3} as $units |
if .[-1:] == "i" then
{base: 1024, str: .[:-1]}
else
{base: 1000, str: .}
end |
.str[-1:] as $unit |
if $units | has($unit) then
pow(.base; $units[$unit]) * (.str[:-1] | tonumber)
elif .base == 1000 then
.str | 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