Reputation: 708
I have a very long not sorted log file from many sources, that every line of it has the time it was written in it. i want to sort the log by the time, the problem is there was no convention where to write the time in the line, so I don't have any pattern base on delimiter or location in the line that i can filter by. the only thing i know, is the format of time which is: hh:mm:ss. lines for example:
filename time
time
filename date time
filename exception date time
...
how can i sort the file using this format - even though I don't know exact location in the line? i need to be able to sore it by hours, minutes and seconds
Upvotes: 0
Views: 648
Reputation: 9365
You can use sed
to extract the time (formatted as hh:mm:ss), prepend it at the beginning of the line, then use this new field for sort, and then remove it again with cut, like that:
sed 's/\(.*\)\([0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}\)/\2\t\1\2/' data|
sort -t: -k3,3|cut -f2-
(this will sort by seconds, change this to -k2,2 or -k1,1 to sort by minutes or hours instead)
Upvotes: 2