Skip to content

Instantly share code, notes, and snippets.

@tcely
Last active May 13, 2022 23:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcely/616758453cd85cf8bd08eaac3fb3f2bc to your computer and use it in GitHub Desktop.
Save tcely/616758453cd85cf8bd08eaac3fb3f2bc to your computer and use it in GitHub Desktop.
min & max functions for awk. I was a bit surprised these weren't already in the numeric functions section of the man page already.
#!/usr/bin/awk -f
function limit_seeker(a, b, direction) {
_record=b;
if (isarray(a)) {
for (k in a) {
_record=limit_seeker(a[k], _record, direction);
}
} else {
if ("-" == direction) {
_new_record=_record > a;
} else {
_new_record=a > _record;
}
if (_new_record) {
_record=a;
}
}
return _record;
}
function min(a, b) {
return limit_seeker(a, b, "-");
}
function max(a, b) {
return limit_seeker(a, b, "+");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment