shell 根据时间戳将多个日志文件合并为一个日志文件

xwbd5t1u  于 2022-11-16  发布在  Shell
关注(0)|答案(2)|浏览(194)

我试图找到一个解决方案,快速合并来自2个应用程序服务器的2个日志文件。日志文件如下:

00:00:00,028 DEBUG [com.acme.productionservice...

我想根据时间戳打印日志文件的一行或另一行,例如:
如果文件1具有2行:

00:00:00,028 DEBUG [com.acme.productionservice...
00:00:00,128 DEBUG [com.acme.productionservice...

文件2有3行:

00:00:00,045 DEBUG [com.acme.productionservice...
00:00:00,100 DEBUG [com.acme.productionservice...
00:00:00,150 DEBUG [com.acme.productionservice...

输出应该是

00:00:00,028 DEBUG [com.acme.productionservice...   (file 1)
00:00:00,045 DEBUG [com.acme.productionservice...   (file 2)
00:00:00,100 DEBUG [com.acme.productionservice...   (file 2)
00:00:00,128 DEBUG [com.acme.productionservice...   (file 1)
00:00:00,150 DEBUG [com.acme.productionservice...   (file 2)

我目前知道唯一方法是使用cat file1 file|排序,但这是非常缓慢的GB的日志,我需要一些像阅读2个文件,比较时间戳,并决定打印什么。

yi0zb3m4

yi0zb3m41#

最后我用了

sort -m

我还使用了一个技巧来了解日志来自哪个日志文件,

for a in *.log ; do 
    awk  '$0=FILENAME" "$0' $a > $a.log
do
sort -m -k 2 *.log.log
rsaldnfx

rsaldnfx2#

尝试Super Speedy Syslog Searcher
(假设您有rust installed

cargo install super_speedy_syslog_searcher

然后

s4 log1 log2

但是,Super Speedy Syslog Searcher 期望找到 date 时间戳。如果您可以将日志记录格式从timestamp更改为datestamp,则s4可以对行进行排序和合并。

相关问题