Skip to content

Instantly share code, notes, and snippets.

@4ge32
Last active September 30, 2018 10:05
Show Gist options
  • Save 4ge32/460247d5642a62b5b781c45ff1b9f83d to your computer and use it in GitHub Desktop.
Save 4ge32/460247d5642a62b5b781c45ff1b9f83d to your computer and use it in GitHub Desktop.
#!/bin/bash
# Variable settings
Digit_2=0.005
Digit_1=0.05
atexit() {
[[ -n ${tmpfile-} ]] && rm -f "${tmpfile}"
}
# $1: csv file name
# $2: row of file
average() {
tmp=`tail +2 ${1} | awk -F, "{ sum += $"${2}" } END{print sum/NR }"`
Res=`echo | awk "{printf ${tmp} + ${Digit_2}}"`
echo ${Res} >> ${tmpfile}
printf '%.2f\n' ${Res}
}
median() {
(
csvline=`grep -c '' ${1}`
numStudents=$((${csvline} - 1))
trap atexit EXIT
trap 'trap - EXIT; atexit; exit -1' INT PIPE TERM
tmpfile=$(mktemp)
echo `tail +2 ${1} | cut -d ',' -f${2} | sort -n` >> ${tmpfile}
mid=`expr ${numStudents} / 2`
if [ `expr ${mid} % 2` -eq 1 ]; then
Res=`cat ${tmpfile} | cut -d ' ' -f${mid}`
else
next=$((${mid} + 1))
med1=`cat ${tmpfile} | cut -d ' ' -f${mid}`
med2=`cat ${tmpfile} | cut -d ' ' -f${next}`
med=$((${med1} + ${med2}))
tmp=`echo "scale=2; ${med} / 2" | bc`
Res=`echo | awk "{printf ${tmp} + ${Digit_1}}"`
fi
printf '%.1f\n' ${Res}
rm -f ${tmpfile}
)
}
variance() {
if [ -r ${tmpfile} ]; then
row=$((${2} - 1))
ave=`sed -n ${row}p ${tmpfile}`
else
ave=`average ${1} ${2}`
fi
tmp=`tail +2 ${1} | awk -F, "{ sum += ( ${ave} - $"${2}")^2 } END{print sum/NR}"`
Res=`echo | awk "{printf ${tmp} + ${Digit_2}}"`
printf '%.2f\n' ${Res}
}
print_header() {
printf "%10s %10s %10s %10s\n" Subject Mean Median Variance
}
print_res() {
printf "%10s " ${1}
}
print_header
trap atexit EXIT
trap 'trap - EXIT; atexit; exit -1' INT PIPE TERM
tmpfile=$(mktemp)
RowNum=`head -n 1 ${1} | awk -F ',' '{print NF}'`
Num=2
for row in `seq 2 $RowNum`
do
Subject=`head -n 1 ${1} | cut -d ',' -f ${row}`
print_res ${Subject}
print_res `average ${1} ${row}`
print_res `median ${1} ${row}`
print_res `variance ${1} ${row}`
echo
done
exit 0
ID Mathmatics Science English Japanese History Geography
A0001 55 63 65 77 61 46
A0002 61 78 91 88 89 90
A0003 29 0 49 42 52 37
A0004 36 40 43 50 49 42
A0005 72 66 89 92 70 65
A0006 45 55 68 71 81 67
A0007 74 75 53 54 62 0
A0008 56 80 87 97 94 87
A0009 70 85 55 51 56 62
A0010 79 98 82 99 91 78
A0011 31 47 52 46 54 44
A0012 39 53 71 68 82 71
A0013 0 37 0 40 46 40
A0014 94 82 67 58 63 58
A0015 42 44 64 67 77 80
A0016 73 70 81 83 72 70
A0017 78 96 61 56 58 59
A0018 52 64 76 94 68 45
A0019 68 68 95 85 73 64
A0020 65 60 85 72 60 51
A0021 50 77 78 73 69 54
A0022 47 57 74 64 79 73
A0023 67 89 58 63 66 52
A0024 64 61 93 69 69 50
A0025 60 71 73 75 67 55
A0026 58 67 98 79 86 75
A0027 49 50 59 66 75 75
A0028 87 93 79 61 65 48
ID Mathmatics Science English Japanese History Geography
A0001 64 68 95 85 96 93
A0002 65 59 78 76 74 71
A0003 70 55 84 75 75 82
A0004 77 82 57 59 69 63
A0005 74 60 92 84 76 74
A0006 71 92 58 60 68 64
A0007 56 54 66 69 86 85
A0008 40 0 73 68 75 97
A0009 64 65 68 86 64 56
A0010 76 58 93 99 82 83
A0011 60 63 95 88 98 92
A0012 58 78 67 78 65 61
A0013 55 50 78 65 100 80
A0014 88 87 73 55 71 51
A0015 28 45 50 50 51 44
A0016 65 77 60 96 64 57
A0017 47 40 71 71 77 90
A0018 38 36 68 66 78 94
A0019 32 37 63 72 81 99
A0020 0 47 75 70 82 92
A0021 62 64 88 75 92 93
A0022 48 46 52 40 49 54
A0023 72 67 98 70 75 70
A0024 98 97 70 57 65 49
A0025 68 69 91 74 79 77
A0026 51 52 76 67 93 86
A0027 69 59 79 69 57 55
A0028 70 68 86 72 74 58
A0029 81 80 72 56 67 46
A0030 43 44 46 44 43 41
A0031 71 56 96 73 76 81
A0032 46 42 66 72 80 96
A0033 71 76 90 81 83 74
A0034 85 89 79 62 71 44
A0035 62 75 59 81 73 65
A0036 35 33 52 48 53 52
A0037 50 41 0 52 50 47
A0038 94 88 60 61 66 48
A0039 81 75 49 46 52 47
A0040 84 96 81 77 97 90
A0041 53 51 69 64 99 75
A0042 96 71 50 47 55 48
A0043 60 63 92 73 88 88
A0044 86 95 63 58 56 53
A0045 68 66 83 68 62 72
A0046 68 62 85 76 80 75
A0047 31 48 53 49 47 43
A0048 61 64 94 93 91 91
A0049 57 78 66 87 73 62
A0050 65 72 74 89 74 69
A0051 73 70 94 65 61 67
A0052 63 73 80 78 87 95
A0053 55 54 61 68 95 76
A0054 67 61 82 89 98 89
A0055 63 77 97 73 97 98
A0056 60 74 77 75 73 73
A0057 72 82 55 55 60 61
A0058 67 56 76 74 63 66
A0059 75 98 56 62 63 58
A0060 52 53 78 68 95 78
A0061 59 65 83 75 99 100
A0062 75 84 55 63 60 56
A0063 87 79 65 54 70 45
A0064 54 49 59 67 94 80
A0065 92 86 81 92 90 96
A0066 76 66 89 91 78 79
A0067 53 52 69 66 84 76
A0068 79 84 54 64 70 71
A0069 90 71 42 0 46 54
A0070 100 81 71 53 68 50
A0071 69 64 86 83 76 87
A0072 36 38 47 51 54 0
A0073 59 79 75 77 58 64
A0074 67 73 99 71 72 69
A0075 56 67 64 79 67 68
A0076 78 91 56 60 59 72
A0077 97 61 48 52 54 42
A0078 42 39 62 70 76 94
A0079 61 62 64 85 69 59
A0080 49 43 44 49 53 39
A0081 83 94 87 73 85 95
A0082 91 93 77 58 61 51
A0083 58 70 62 74 72 60
A0084 72 68 84 71 57 66
A0085 66 57 80 91 93 97
A0086 80 99 100 88 89 98
A0087 74 65 90 95 77 78
ID Mathmatics Science English Japanese History Geography
A0001 45 46 59 68 88 76
A0002 51 65 59 86 64 48
A0003 79 82 74 60 73 55
A0004 65 57 89 70 82 55
A0005 46 42 70 62 86 92
A0006 82 97 66 57 76 54
A0007 33 45 60 71 90 78
A0008 63 73 79 85 89 71
A0009 46 48 73 66 97 66
A0010 31 43 42 45 53 39
A0011 69 71 94 96 90 72
A0012 40 50 77 71 93 66
A0013 58 65 68 79 83 50
A0014 38 53 66 66 97 63
A0015 95 96 69 53 69 47
A0016 75 96 65 59 77 48
A0017 77 87 94 91 98 77
A0018 41 39 62 65 91 77
A0019 45 48 60 68 100 72
A0020 73 85 73 54 83 45
A0021 24 44 41 43 55 40
A0022 58 76 91 100 100 74
A0023 48 74 76 72 66 43
A0024 90 98 99 77 92 99
A0025 68 60 82 94 89 65
A0026 87 94 64 60 65 46
A0027 55 60 86 74 93 73
A0028 30 43 46 41 58 40
A0029 54 65 88 94 99 97
A0030 83 100 61 47 85 46
A0031 59 56 89 97 96 79
A0032 30 37 40 0 56 38
A0033 47 75 63 73 67 59
A0034 68 75 95 78 88 66
A0035 63 70 96 72 88 71
A0036 71 75 80 69 71 52
A0037 44 54 68 67 97 71
A0038 66 67 91 86 87 67
A0039 80 84 71 58 80 56
A0040 74 81 77 61 70 57
A0041 67 80 92 76 89 64
A0042 56 64 61 99 62 53
A0043 65 97 55 56 66 44
A0044 76 71 51 50 74 33
A0045 51 58 99 94 100 81
A0046 34 47 63 62 90 74
A0047 68 63 83 69 81 57
A0048 49 57 87 72 100 74
A0049 50 67 93 73 96 80
A0050 73 92 76 51 80 48
A0051 29 35 0 40 57 42
A0052 60 66 82 69 69 49
A0053 47 61 76 95 82 50
A0054 72 77 90 97 91 72
A0055 66 100 54 55 84 47
A0056 67 70 98 68 78 46
A0057 51 67 61 88 72 44
A0058 49 65 79 92 99 74
A0059 64 59 93 70 63 49
A0060 51 70 77 74 82 58
A0061 48 72 84 79 98 89
A0062 50 77 91 97 99 82
A0063 62 62 96 84 86 68
A0064 22 36 46 39 59 41
A0065 54 66 82 98 95 73
A0066 38 36 75 70 90 81
A0067 63 93 51 55 77 51
A0068 77 99 66 54 68 53
A0069 71 69 87 74 91 61
A0070 67 64 83 81 87 67
A0071 32 0 67 64 91 76
A0072 62 85 48 53 75 49
A0073 54 79 90 90 95 73
A0074 41 49 71 63 98 62
A0075 61 80 81 64 85 48
A0076 50 68 92 87 94 87
A0077 25 41 44 42 59 37
A0078 70 73 80 71 75 54
A0079 61 61 94 68 79 46
A0080 96 60 54 59 72 42
A0081 59 72 63 82 85 55
A0082 28 0 45 42 61 35
A0083 74 92 57 58 64 59
A0084 49 67 65 77 83 56
A0085 52 79 62 91 69 58
A0086 35 40 70 70 86 84
A0087 60 76 87 82 87 62
A0088 64 63 85 65 63 52
A0089 65 86 52 46 74 50
A0090 39 49 58 65 92 68
A0091 70 76 79 67 80 54
A0092 71 88 48 57 79 44
A0093 89 84 58 49 74 53
A0094 75 95 79 89 99 88
A0095 81 91 67 52 79 51
A0096 72 61 78 95 90 69
A0097 57 58 58 92 65 47
A0098 76 82 97 74 95 85
A0099 53 78 95 71 93 76
A0100 55 56 75 73 71 51
A0101 56 66 78 98 61 43
A0102 67 73 88 90 89 63
A0103 76 64 53 61 63 42
A0104 36 51 75 66 98 65
A0105 69 74 85 83 88 68
A0106 43 52 69 62 95 64
A0107 98 99 84 89 93 75
A0108 81 88 100 93 100 75
A0109 35 51 64 64 92 61
A0110 33 46 67 63 89 75
A0111 54 62 72 72 77 45
A0112 79 65 57 48 67 41
A0113 69 94 52 54 71 58
A0114 52 79 63 76 79 55
A0115 91 91 65 56 84 45
A0116 62 56 80 75 92 65
A0117 44 38 78 67 88 83
A0118 46 53 74 68 94 63
A0119 72 55 95 63 72 60
A0120 57 68 81 93 93 95
A0121 56 80 85 73 96 78
A0122 70 83 53 57 68 59
A0123 84 63 52 50 85 0
A0124 100 62 49 52 81 39
A0125 42 40 67 67 91 73
A0126 42 54 78 66 94 69
A0127 0 38 38 44 57 0
A0128 47 59 60 80 85 52
A0129 57 78 69 88 86 57
A0130 69 81 56 51 70 61
A0131 37 52 72 69 99 69
A0132 72 95 56 50 68 50
A0133 93 61 49 52 84 38
A0134 53 71 57 99 73 43
A0135 65 69 99 78 90 70
A0136 60 58 81 74 97 76
A0137 52 68 64 100 62 60
A0138 59 69 74 77 83 56
A0139 85 97 86 76 98 78
A0140 64 89 55 47 82 53
A0141 70 55 98 75 86 70
A0142 78 78 50 49 76 41
A0143 40 44 76 70 87 93
A0144 59 66 71 84 78 44
A0145 26 47 47 45 60 36
A0146 74 98 92 89 96 91
A0147 29 42 47 43 60 36
A0148 61 90 55 60 75 52
A0149 55 64 83 85 97 80
A0150 36 53 73 64 94 64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment