Last active
May 13, 2022 23:28
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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