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
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.
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.
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.
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.
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
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:
awk -F= '...' test.txt
: Invoke awk on test.txt, using =
as the field separatorBEGIN { max = -inf }
: Initialise max
to something that will always be smaller than whatever you're reading.{ 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
)END { print line }
: Finally, print the line we remembered while reading the input.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With