Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 592
Eman Avatar asked Aug 06 '13 05:08

Eman


People also ask

How do I find the maximum value of a file in Linux?

Explanation. grep -Eo '[0-9]+' file prints all matches of positive decimal integer numbers in the file. Each match will be printed in a different line, as per the -o flag. sort -rn sorts the list numerically and in reverse, so that the first number is the biggest.

How do I find the max value of a file in Unix?

The -v max=0 sets the variable max to 0 , then, for each line, the first field is compared to the current value of max . If it is greater, max is set to the value of the 1st field and want is set to the current line. When the program has processed the entire file, the current value of want is printed.

How can I get the size of a file in a bash script?

Another method we can use to grab the size of a file in a bash script is the wc command. The wc command returns the number of words, size, and the size of a file in bytes.

What is $@ in bash script?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.


2 Answers

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]}
3.0
$ echo ${var[3]}
89.5039
like image 179
devnull Avatar answered Oct 06 '22 07:10

devnull


For very large files, using sort will be quite slow. In this case, it's better to use something like awk, which needs only one pass:

$ awk -F= 'BEGIN { max = -inf } { if ($3 > max) { max = $3; line = $0 } } END { print line }' test.txt
log2c=3.0 rate=89.5039

The time complexity of this operation is linear, while the space complexity is constant (and small). Explanation:

  1. awk -F= '...' test.txt: Invoke awk on test.txt, using = as the field separator
  2. BEGIN { max = -inf }: Initialise max to something that will always be smaller than whatever you're reading.
  3. { if ($3 > max) { max = $3; line = $0; } }: For each input line, if max is less than the value of the third field ($3), then update it and remember the value of the current line ($0)
  4. END { print line }: Finally, print the line we remembered while reading the input.
like image 7
Will Vousden Avatar answered Oct 06 '22 07:10

Will Vousden