Narek At Narek At - 4 months ago 10
Linux Question

Sort lines in file with highest value of specified pattern

In a log file which contains some performance info, I would like to sort lines in decreasing order depending from elapsed time(real-time) written in each line. For example:

Operation1 cpu-time = 10 real-time = 4 malloc = 1006
Operation2 cpu-time = 12 real-time = 5 malloc = 1002
// other info1 (without time or memory measurments)
Operation3 cpu-time = 9 real-time = 10 malloc = 1003
// other info2 (without time or memory measurments)


And the output should be like this (sorted in decreasing order of real-time) :

Operation3 cpu-time = 9 real-time = 10 malloc = 1003
Operation2 cpu-time = 12 real-time = 5 malloc = 1002
Operation1 cpu-time = 10 real-time = 4 malloc = 1006
// other info1 (without time or memory measurments)
// other info2 (without time or memory measurments)


Note that unnecessary lines shifted to the end of file

Answer

Use sort:

sort -rn -k7,7

n - number sort

r - reverse

-k7,7 - use 7th field only for sorting (fields are by default separated with spaces)