如何按日期对多个日志文件的输出进行排序
我从几个不同的日志文件中获得了输出:
logfile3
2010/07/21 15:28:52 INFO xxx
2010/07/21 15:31:25 INFO xxx
2010/07/21 15:31:25 DEBUG xxx
logfile1
2010/07/21 19:28:52 INFO xxx
2010/07/21 19:31:25 INFO xxx
2010/07/21 19:31:25 DEBUG xxx
logfile2
2010/07/21 13:28:52 INFO xxx
2010/07/21 13:31:25 INFO xxx
2010/07/21 13:31:25 DEBUG xxx
我想按日期对此输出进行排序,但将日志文件的名称保留在日志行上方,因此它应该如下所示:
logfile2
2010/07/21 13:28:52 INFO xxx
2010/07/21 13:31:25 INFO xxx
2010/07/21 13:31:25 DEBUG xxx
logfile3
2010/07/21 15:28:52 INFO xxx
2010/07/21 15:31:25 INFO xxx
2010/07/21 15:31:25 DEBUG xxx
logfile1
2010/07/21 19:28:52 INFO xxx
2010/07/21 19:31:25 INFO xxx
2010/07/21 19:31:25 DEBUG xxx
Do you haveany idea how to sort out such with bash 命令、sed 还是 awk? 多谢!
更新: 的来源
for i in $( find log/ -iname *debug*.log -size +0 );do
if [ `grep -c 'ERROR' $i` -gt 0 ];then
echo -e "\n$i"
grep 'ERROR' --color=auto -A 5 -B 5 $i
fi
done
这是输出Martin
I have got output from several different log files:
logfile3
2010/07/21 15:28:52 INFO xxx
2010/07/21 15:31:25 INFO xxx
2010/07/21 15:31:25 DEBUG xxx
logfile1
2010/07/21 19:28:52 INFO xxx
2010/07/21 19:31:25 INFO xxx
2010/07/21 19:31:25 DEBUG xxx
logfile2
2010/07/21 13:28:52 INFO xxx
2010/07/21 13:31:25 INFO xxx
2010/07/21 13:31:25 DEBUG xxx
I would like to sort this output by date, but keep the name of the logfile above the log lines, so it should look like:
logfile2
2010/07/21 13:28:52 INFO xxx
2010/07/21 13:31:25 INFO xxx
2010/07/21 13:31:25 DEBUG xxx
logfile3
2010/07/21 15:28:52 INFO xxx
2010/07/21 15:31:25 INFO xxx
2010/07/21 15:31:25 DEBUG xxx
logfile1
2010/07/21 19:28:52 INFO xxx
2010/07/21 19:31:25 INFO xxx
2010/07/21 19:31:25 DEBUG xxx
Do you have any idea how to sort output like this with bash commands, sed or awk?
Thanks a lot!
UPDATE:
This is the source of the output
for i in $( find log/ -iname *debug*.log -size +0 );do
if [ `grep -c 'ERROR' $i` -gt 0 ];then
echo -e "\n$i"
grep 'ERROR' --color=auto -A 5 -B 5 $i
fi
done
Martin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您也许能够从中获得满意的结果(只要您的文件名都不包含冒号):
每一行都会以文件名开头。您的输出将如下所示:
您可以使用 AWK 将其重新格式化为示例中显示的格式:
以下是对脚本的几项改进,以防您仍然使用它:
You may be able to get satisfactory results from this (as long as none of your filenames contain colons):
Each line will be prepended by the filename. Your output will look something like this:
You can use AWK to reformat that into the format that you show in your example:
Here are several improvements to your script, in case you still use it:
如果你已经在文件(或脚本输出)中输出了,我会选择 Perl:
或者,更干净一点:
If you have you output already in a file (or script output) I'd go Perl:
Or, a little more clean:
谢谢大家。
我改进了 Dennis Williamson 的脚本,以按日期对错误进行排序。每个内部有错误的日志文件都保存在以上次发生错误的时间戳命名的文件中。这些文件随后被分类并放在一起。可能有比使用临时文件更干净的解决方案。
感谢意见和改进建议!
Thank you all.
I improved script from Dennis Williamson to sort errors by date. Each log file with error inside is saved in file named by the timestamp of last error occured. These files are later sorted and put together. There may be cleaner solutions for that than to use of temp files.
Opinions and suggestions for improvement appreciated!
您可以尝试基于 Rust 的工具超快速系统日志搜索器
(假设您有rust 安装)
然后
但是...
s4
无法输出该方法。s4
可以在日志消息之前写入日志文件的名称。You could try rust-based tool Super Speedy Syslog Searcher
(assuming you have rust installed)
then
HOWEVER...
s4
cannot output that approach.s4
can write the name of the logfile to precede the log message.