我正在尝试根据时间戳对一系列日志进行排序。通过将文件读入临时数组,我只能通过sort()函数按月排序,但我想实现一个正则表达式来抓取字符串的一部分进行排序。
我用来抓取线条的正则表达式是:
LOGGER_LINE = /([a-zA-Z]{3} \d{1,2}, \d{4} \d{1,2}:\d{2}:\d{2} (AM|PM) (\(SEVERE\)|\(WARNING\)).*)/
我可以产生这样的输出:
[FILENAME]
Feb 16, 2014 1:00:10 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 16, 2014 1:00:10 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 16, 2014 1:00:10 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 16, 2014 1:00:10 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 16, 2014 1:00:20 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 17, 2014 1:00:00 AM (SEVERE) Thread: 18 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 17, 2014 1:00:00 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 17, 2014 1:00:00 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 17, 2014 1:00:00 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 17, 2014 1:00:10 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 17, 2014 1:00:10 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 17, 2014 1:00:10 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 17, 2014 1:00:10 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 17, 2014 1:00:20 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 18, 2014 1:00:00 AM (SEVERE) Thread: 18 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 18, 2014 1:00:00 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 18, 2014 1:00:00 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 18, 2014 1:00:00 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 18, 2014 1:00:10 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 18, 2014 1:00:10 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 18, 2014 1:00:10 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 18, 2014 1:00:10 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 18, 2014 1:00:20 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
[FILENAME_2]
Feb 14, 2014 9:29:01 AM (WARNING) Thread: 26 [com.queue.lookupUtility.keyValueListAsMap] Failed to process standard key/value pair format.
Feb 14, 2014 9:33:50 AM (WARNING) Thread: 26 [com.queue.lookupUtility.keyValueListAsMap] Failed to process standard key/value pair format.
Feb 14, 2014 10:22:31 AM (WARNING) Thread: 27 [com.queue.lookupUtility.keyValueListAsMap] Failed to process standard key/value pair format.
Feb 14, 2014 10:39:31 AM (WARNING) Thread: 28 [com.queue.lookupUtility.keyValueListAsMap] Failed to process standard key/value pair format.
Feb 14, 2014 10:40:31 AM (WARNING) Thread: 28 [com.queue.lookupUtility.keyValueListAsMap] Failed to process standard key/value pair format.
Feb 18, 2014 8:43:45 AM (WARNING) Thread: 13 [com.nioHandler]Closing socket to endpoint Address[127.0.0.1:5703], Cause:java.io.EOFException
然而,我的最终目标是按降序对时间戳及其相应的错误进行排序。
有没有办法可以根据LOGGER_LINE
正则表达式匹配的时间戳对行进行排序?关于对时间戳进行排序的其他建议非常好。
答案 0 :(得分:4)
你不应该重新发明轮子。 time
库解析您拥有的字符串。例如,给定以下行:
l = "Feb 16, 2014 1:00:10 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error"
时间可以像这样提取:
require "time"
Time.parse(l)
# => 2014-02-16 01:00:10 +0900
所以,如果你有一个数组说array_of_lines
这样的行,你可以这样做:
require "time"
array_of_lines.sort_by{|l| Time.parse(l)}.reverse