-
Notifications
You must be signed in to change notification settings - Fork 0
/
group-and-termgraph
executable file
·40 lines (33 loc) · 1.28 KB
/
group-and-termgraph
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/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