Home > database >  How to sort lines based on variable:value using linux
How to sort lines based on variable:value using linux

Time:11-25

I have few lines like below were I need to sort and print the lines based on variable:value

15:16:31.640 [Executor task launch worker for task 85] INFO  IAME- Sum id:705 SignatureId : EDXGH  Events:1320 Time Taken By All Events:11 eps:12000.0 Process Time on Average (in ms) for each event:0.008333333333333333
15:16:31.640 [Executor task launch worker for task 85] INFO  IAME- Sum id:231 SignatureId : EDXGH  Events:60 Time Taken By All Events:13 eps:6000.0 Process Time on Average (in ms) for each event:0.016666666666666666
15:16:31.640 [Executor task launch worker for task 85] INFO  IAME- Sum id:644 SignatureId : EDXGH  Events:1320 Time Taken By All Events:909 eps:14666.666666666666 Process Time on Average (in ms) for each event:0.006818181818181818

I am trying to sort the lines based on "Time Taken By All Events:909" I am expecting to see the output as below

15:16:31.640 [Executor task launch worker for task 85] INFO  IAME- Sum id:644 SignatureId : EDXGH  Events:1320 Time Taken By All Events:909 eps:14666.666666666666 Process Time on Average (in ms) for each event:0.006818181818181818
15:16:31.640 [Executor task launch worker for task 85] INFO  IAME- Sumid:768 SignatureId : EDXGH Events:1320 Time Taken By All Events:13 eps:10153.846153846152 Process Time on Average (in ms) for each event:0.009848484848484848
15:16:31.640 [Executor task launch worker for task 85] INFO  IAME- Sum id:705 SignatureId : EDXGH  Events:1320 Time Taken By All Events:11 eps:12000.0 Process Time on Average (in ms) for each event:0.008333333333333333

Any help on how to get this using linux commands

CodePudding user response:

This is the "decorate-sort-undecorate" pattern: it extracts the number you want to sort by, puts it at the front of the line, sorts, then removes the number

paste <(grep -oP 'Time Taken By All Events:\K\d ' file) file \
| sort -nr -k1,1 \
| cut -f2-

I assume every line contains the "time taken by all events" -- the paste output will be wrong if that assumption is false.

CodePudding user response:

Another form of decorate-sort-undecorate:

sed -E 's/^(.*:)([^:]*)$/\2 \1/' file | sort -nr -k1,1 | sed -E 's/^([^[:space:]]*)(.*)/\2\1/'
  • Related