Skip to content

Instantly share code, notes, and snippets.

@unot
Created November 10, 2015 06:08
Show Gist options
  • Save unot/9b7b64eeda09d8df0ac5 to your computer and use it in GitHub Desktop.
Save unot/9b7b64eeda09d8df0ac5 to your computer and use it in GitHub Desktop.
calculate mean values of multiple Lab files.
#!/bin/bash
# calculate mean values of Lab
# $ cat file1
# L1_1 a1_1 b1_1
# L1_2 a1_2 b1_2
# $ cat file2
# L2_1 a2_1 b2_1
# L2_2 a2_2 b2_2
# $ cat file3
# L3_1 a3_1 b3_1
# L3_2 a3_2 b3_2
# $ bash meanLab.sh file1 file2 file3
# (L1_1 + L2_1 + L3_1)/3 (a1_1 + a2_1 + a3_1)/3 (b1_1 + b2_1 + b3_1)/3
# (L1_2 + L2_2 + L3_2)/3 (a1_2 + a2_2 + a3_2)/3 (b1_2 + b2_2 + b3_2)/3
set -eu -o pipefail
if [ $# -lt 2 ]; then
echo "USAGE: $0 file1 ... > mean_file"
exit 1
fi
awk '{L=FNR;for(i=1;i<=NF;++i) SUM[FNR,i]+=$i}END{for(j=1;j<=L;++j){print SUM[j,1]/(NR/L),SUM[j,2]/(NR/L),SUM[j,3]/(NR/L)}}' "$@"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment