-
Notifications
You must be signed in to change notification settings - Fork 0
/
mext
executable file
·94 lines (80 loc) · 2.56 KB
/
mext
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/sh
# This program is part of Aspersa (http://code.google.com/p/aspersa/)
# Author: Baron Schwartz
usage() {
echo "Usage: $0 [-r] -- commandline"
echo " -r Relative: subtract each column from the previous column."
echo " -- Option separator; after this, all arguments are passed"
echo " to the program whose output mext will columnize."
echo "mext columnizes repeated output from a program like mysqladmin extended:"
echo " mext -r -- mysqladmin ext -i10 -c3"
echo "You can also work with data from a file:"
echo " mext -r -- cat mysqladmin-output.txt"
exit 1
}
if [ -z "$1" ]; then
usage;
fi
FILE=/tmp/mext_temp_file;
NUM=0;
REL=0;
rm -f $FILE*;
# Command-line parsing.
args=`getopt -u -n mext r "$@"`;
if [ "$?" = "1" ]; then
usage;
fi
set -- $args
for o; do
case "$o" in
-r) REL="1"; shift;;
--) shift; break;;
esac
done
if [ -z "$1" ]; then
usage;
fi
# Split the output on empty lines and put each into a different file; eliminate
# lines that don't have "real" content.
$@ | grep -v '+' | grep -v Variable_name | sed 's/|//g' \
| while read line; do
if [ "$line" = "" ]; then
NUM=`expr $NUM + 1`;
echo "" > $FILE$NUM;
fi
echo "$line" >> $FILE$NUM;
done
# Count how many files there are and prepare to format the output
SPEC="%-33s %13d"
AWKS=""
NUM=`ls $FILE* | wc -l`;
# The last file will be empty...
NUM=`expr $NUM - 3`;
# Join each file with the next file, joining on the first field. Build a printf
# spec and awk spec at the same time.
for i in `seq 0 $NUM`; do
NEXTFILE=`expr $i + 1`;
# Sort each file and eliminate empty lines, so 'join' doesn't complain.
sort $FILE$i | grep . > $FILE$i.tmp;
mv $FILE$i.tmp $FILE$i;
sort $FILE${NEXTFILE} | grep . > $FILE${NEXTFILE}.tmp;
mv $FILE${NEXTFILE}.tmp $FILE${NEXTFILE};
# Join the files together. This gets slow O(n^2) as we add more files, but
# this really shouldn't be performance critical.
join $FILE$i $FILE${NEXTFILE} | grep . > $FILE;
# Find the max length of the [numeric only] values in the file so we know how
# wide to make the columns
MAXLEN=`awk '{print $2}' $FILE${NEXTFILE} | grep -v '[^0-9]' | awk '{print length($1)}' | sort -rn | head -n1`
mv $FILE $FILE${NEXTFILE};
SPEC="$SPEC %${MAXLEN}d";
if [ "$REL" = "1" ]; then
AWKS="$AWKS, \$`expr $i + 3` - \$`expr $i + 2`";
else
AWKS="$AWKS, \$`expr $i + 3`";
fi
done
# Print output
AWKCMD="printf(\"$SPEC\n\", \$1, \$2$AWKS);";
awk "{$AWKCMD}" $FILE`expr $NUM + 1`;
# Remove all temporary files.
rm -f $FILE*;