#!/usr/bin/env bash usage() { echo "Usage: command | group-and-termgraph takes data from STDIN groups it and graphs it in your terminal using termgraph: https://github.com/mkaz/termgraph This can be thought of as a fancy 'sort | uniq -c | sort'" } if [[ "$1" == '-h' ]]; then usage exit 0 fi set -e set -o pipefail havecmd -v rev || exit $? havecmd -v awk || exit $? havecmd -v sed || exit $? havecmd -V 'Install from https://github.com/purarue/chomp' 'chomp' || exit $? havecmd -V 'Install with "pip install termgraph"' 'termgraph' || exit $? # this uses '|@|' as a special marker to replace commas with # and then replaces them afterwards once things have been graphed # # remove extra spaces, count using 'uniq -c', and replace commas (used as a delimtier for termgraph) with '|@|' chomp | sort | uniq -c | chomp | sed -e 's/,/|@|/g' | # move the first column (count) to the end, print the rest of the columns first awk '{for(i=2;i<=NF;i++){printf "%s ", $i}; printf $1; printf "\n"}' | # replace the last space with a comma (the termgraph delimiter) rev | sed -e 's/ /,/' | rev | # graph with termgraph termgraph --delim ',' "$@" | # remove extra lines termgraph prints, reset commas that were there in original text # sort by last column chomp | sed -e 's/|@|/,/g' | sort-by-last-col