Last active
December 29, 2015 10:59
-
-
Save doi-t/7660870 to your computer and use it in GitHub Desktop.
awkで算出した平均等をgnuplotでグラフ化する
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 | |
#log format:[log_msg time] | |
calc_avg(){ | |
awk 'BEGIN{ OFS="\t"; sum=0; max=0; } | |
NR==1 { min=$2; } | |
{ sum+=$2; if($2>max){max=$2}; if(min>$2){min=$2}; } | |
END{ print($1, sum, NR, max, min, sum/NR) }' | |
} | |
mkgraph(){ | |
dest_dir="graph_png" | |
[ ! -e "$dest_dir" ] && mkdir "$dest_dir" | |
gnuplot <<- EOF | |
set grid y | |
set terminal png | |
set boxwidth 0.5 | |
set yrange [0:] | |
set xtics rotate by -15 | |
set style fill solid border lc rgb "black" | |
set output "$dest_dir/$1_sum.png" | |
plot "$2" using (\$0*4+0):2:xtic(1) with boxes lw 2 lc rgb "light-cyan" title "sum" | |
set output "$dest_dir/$1_count.png" | |
plot "$2" using (\$0*4+0):3:xtic(1) with boxes lw 2 lc rgb "light-cyan" title "count" | |
set output "$dest_dir/$1_max.png" | |
plot "$2" using (\$0*4+0):4:xtic(1) with boxes lw 2 lc rgb "light-cyan" title "max" | |
set output "$dest_dir/$1_min.png" | |
plot "$2" using (\$0*4+0):5:xtic(1) with boxes lw 2 lc rgb "light-cyan" title "min" | |
set output "$dest_dir/$1_avg.png" | |
plot "$2" using (\$0*4+0):6:xtic(1) with boxes lw 2 lc rgb "light-cyan" title "avg" | |
EOF | |
} | |
[ $# -ne 1 -o ! -e "$1" ] && echo "Usage:$0 [log_file]" && exit 1 | |
plotfile="sample.plot" | |
echo -e "#label\tsum\tcount\tmax\tmin\tavg" > $plotfile | |
grep "hoge" $1 | calc_avg >> $plotfile | |
grep "fuga" $1 | calc_avg >> $plotfile | |
grep "foo" $1 | calc_avg >> $plotfile | |
grep "bar" $1 | calc_avg >> $plotfile | |
mkgraph "sample" $plotfile |
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
foo 10 | |
hoge 8.431 | |
fuga 0.542 | |
fuga 1.021 | |
fuga 0.732 | |
fuga 0.981 | |
foo 30 | |
bar 5.679 | |
hoge 9.321 | |
hoge 10.021 | |
bar 6.091 | |
foo 60 | |
hoge 11.234 | |
fuga 1.329 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment