Eman Eman - 6 months ago 16
Bash Question

how to get max number from a file with Linux bash shell scripts

How to get the maximum "rate" and the corresponding "log2c" value from a file as follows?
e.g: the max rate is 89.5039 , and log2c 3.0 . thanks a lot.

log2c=5.0 rate=88.7619
log2c=-1.0 rate=86.5412
log2c=11.0 rate=86.1482
log2c=3.0 rate=89.5039
log2c=-3.0 rate=85.5614
log2c=9.0 rate=81.4302


Use sort:

sort -t= -nr -k3 inputfile | head -1

For the given input, it'd return:

log2c=3.0 rate=89.5039

If you want to read the values into variables, you can use the builtin read:

$ IFS=$' =' read -a var <<< $(sort -t= -nr -k3 inputfile | head -1)
$ echo ${var[1]}
$ echo ${var[3]}