在Java中使用特定时间戳之后,grep log4j [英] Grep log4j for lines after certain timestamp in Java
问题描述
我必须计算某个字符串在log4j文件中出现的次数。我有一个开始日期,我想从 grep
ing(也许 sed
或 awk
即使我不知道是否更好地捕获来自 Runtime.getRuntime()。exec(...)
命令与读取和解析(通过正则表达式和字符串操作)当前log4j文件,我不知道如何 grep
像这样的文件:
2016-07-29 15:13: 08,725:INFO [main] .....
2016-07-29 15:13:08,817:INFO [main] ....
2016-07-29 15:13:08,901:INFO [main] ...
但仅限于 2016-07-29 15 :13:08,818
例如
预先感谢!
如果你有一个完全匹配你想分割输出,按照另一问题在这里你可以使用sed来仅输出该时间戳之后的行。发现准确的匹配可能意味着逐渐让你的时间戳更宽松,例如,删除最后一个字符,直到你得到一个匹配/匹配,然后解析这些匹配来决定哪个是第一个。
如果你想在bash中这样做,考虑这个问题;具体将日期转换为unix时间戳,然后比较结果值。其余的应该是直截了当的;同样在Java中,提取日期和比较它们不应该太具有挑战性。使用您更熟悉的语言,除非您拥有学习体验的带宽。
I have to count how many occurrences of certain strings appear in a log4j file. I have a start date from which I want to start grep
ing (maybe sed
or awk
?) in a Java class during a test.
Even if I don't know if it's better to capture the grep result from a Runtime.getRuntime().exec(...)
command versus reading and parsing (via regex and string manipulation) the current log4j file, I don't know how to grep
a file like this:
2016-07-29 15:13:08,725: INFO [main] .....
2016-07-29 15:13:08,817: INFO [main] ....
2016-07-29 15:13:08,901: INFO [main] ...
but only after 2016-07-29 15:13:08,818
for example
Thanks in advance!
If you have an exact match from which you want divide the output, as per another question here you can use sed to only output the lines from and after that timestamp. Finding that exact match on which to break probably means progressively making your timestamp more lenient e.g. dropping the last character until you get a match/matches, then parsing those matches to decide which is the first after.
If you'd like to do that in bash, consider this question; specifically converting the dates to unix timestamps and then comparing the resultant values. The rest should be straightforward; equally in Java, extracting the date and comparing them should not be too challenging. Use the language that you're more familiar with unless you have the bandwidth for a learning experience.
这篇关于在Java中使用特定时间戳之后,grep log4j的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!