Skip to content

Instantly share code, notes, and snippets.

@graysky2
Last active April 3, 2017 08:02
Show Gist options
  • Save graysky2/7d0447e04dcf2ac638cb to your computer and use it in GitHub Desktop.
Save graysky2/7d0447e04dcf2ac638cb to your computer and use it in GitHub Desktop.
find the median value from a i7z log file using awk
#!/bin/bash
# credit for awk magic goes to
# http://stackoverflow.com/questions/6166375/median-of-column-with-awk
[[ -d $XDG_RUNTIME_DIR ]] &&
file=$XDG_RUNTIME_DIR/.tempfile ||
file=/tmp/.tempfile
# check deps
command -v awk >/dev/null 2>&1 || {
echo " I require awk but it's not installed. Aborting!" >&2; exit 1; }
[[ -f "$1" ]] || {
echo 'ERROR'
echo " Usage: $0 LOGFILE"
echo ' Supply a log file you generated from "sudo i7z -w a -l" and try again' ;exit 1; }
# assemble an unpiv view of log file up to 12 cores
awk '{ print $2 }' "$1" > $file
awk '{ print $3 }' "$1" >> $file
awk '{ print $4 }' "$1" >> $file
awk '{ print $5 }' "$1" >> $file
awk '{ print $6 }' "$1" >> $file
awk '{ print $7 }' "$1" >> $file
awk '{ print $8 }' "$1" >> $file
awk '{ print $9 }' "$1" >> $file
awk '{ print $10 }' "$1" >> $file
awk '{ print $11 }' "$1" >> $file
awk '{ print $12 }' "$1" >> $file
awk '{ print $13 }' "$1" >> $file
# remove blank lines from greedy awk line to keep count good
sed -i '/^$/d' $file
# calc the median and count then report them and delete the temp file
median=$(sort -n $file | awk ' { a[i++]=$1; } END { x=int((i+1)/2); if (x < (i+1)/2) print (a[x-1]+a[x])/2; else print a[x-1]; }')
mean=$(awk '{ sum += $1; n++ } END { if (n > 0) print sum / n; }' $file)
min=$(sort -n $file | head -n1)
max=$(sort -n $file | tail -n1)
echo "median : $median"
echo "mean : $mean"
echo "min : $min"
echo "max : $max"
echo "count : $(wc -l $file|awk '{print $1}')"
#[[ -f $file ]] && rm -f $file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment