jackjindtnt / KI

Ki
0 stars 0 forks source link

Find out maximum and minimum value in a column using awk #5

Open jackjindtnt opened 7 years ago

jackjindtnt commented 7 years ago

---------------------without awk----------------------------------- xargs -n1|sort -n|head or tail -1 -------------------------awk---------------------------------------- awk ' NR == 1 { min=max=$1 } { for (i=1;i<=NF;i++) { min = (min < $i ? min : $i) max = (max > $i ? max : $i) } } END { printf "min value = %s\n", (min == "" ? "NaN" : min) printf "max value = %s\n", (max == "" ? "NaN" : max) } ' file

sort -nrk1,1 filename | head -1 | cut -d ' ' -f3 sorting on the first field where, -n specifies numerical sort. -r specifies reverse the sort result. -k1,1 specifies first field for the sorting to occur. Now, after the sorting I am piping the output and just getting the first result which will give me the numerically highest value of column1 in the result. Now, I finally pipe it to cut with the delimiter specified as space and printing the -f3 which is the intended output.

sort -r filename | head -n1 | awk '{print $2}'

reference documents : http://stackoverflow.com/questions/18709507/maximum-and-minimum-using-awk?rq=1 https://en.wikipedia.org/wiki/Bc_(programming_language) http://stackoverflow.com/questions/14222250/floating-point-arithmetic-in-unix-shell-script http://stackoverflow.com/questions/22375771/bash-sed-convert-string-to-int-or-float