Last active
May 16, 2018 11:13
-
-
Save cmbuckley/d94ab47c3676517a528d6d86b049761d to your computer and use it in GitHub Desktop.
Group a list of numbers into discrete buckets
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
# Round to the nearest multiple of "size" | |
function bucket(a) { | |
return int((a + (size/2)) / size) * size; | |
} | |
{ buckets[bucket($1)]++ } | |
END { | |
for (b in buckets) { | |
print b, buckets[b] | |
} | |
} |
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
#!/bin/bash | |
size="$1" | |
shift | |
if ! [ "$size" -eq "$size" ] 2>/dev/null; then | |
echo "Bucket size must be a number" >&2 | |
exit 1 | |
fi | |
awk -v size="$size" -f bucket.awk "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage:
Separated out the Shell script and the Awk script in order to be able to pass awk params to the script, e.g.: