gpt4 book ai didi

ruby - 使用 Ruby 按时间戳对文本文件中的行进行排序

转载 作者:太空宇宙 更新时间:2023-11-03 17:52:25 24 4
gpt4 key购买 nike

我正在尝试根据时间戳对一系列日志进行排序。给定 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 正则表达式匹配的时间戳对行进行排序吗?关于对时间戳进行排序的其他建议会非常好。

最佳答案

您不应该重新发明轮子。 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

关于ruby - 使用 Ruby 按时间戳对文本文件中的行进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21891012/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com