13

I have many log files, like that

log file 1

2016-09-11_19:40:15.537#15437 [INFO] A1
2016-09-11_19:40:15.537#15437 [WARN] A2
2016-09-11_19:40:15.542#15437 [INFO] A3

log file 2

2016-09-11_19:40:15.537#437 [INFO] B1
2016-09-11_19:40:15.540#437 [INFO] B2

I wish I can merge them by script or other method like that sort by time

2016-09-11_19:40:15.537#15437 [INFO] A1
2016-09-11_19:40:15.537#15437 [WARN] A2
2016-09-11_19:40:15.537#437 [INFO] B1
2016-09-11_19:40:15.540#437 [INFO] B2
2016-09-11_19:40:15.542#15437 [INFO] A3

How do I to merge the files with efficient way ? thanks !

Bill Chang
  • 133
  • 1
  • 6

3 Answers3

21

Ref: Merging multiple log files by date including multilines

As mentioned in the above question, if you are certain that all the log lines start with timestamp, you can do:

cat logA.log logB.log | sort -n 

This would not work when there are other lines such as stack trace which do not start with timestamp.
I think you can check out the above question and answers if your considering a similar scenario.

Community
  • 1
  • 1
Ryota
  • 1,157
  • 2
  • 11
  • 27
1

Try rust-based tool Super Speedy Syslog Searcher

(assuming you have rust installed)

cargo install super_speedy_syslog_searcher

then

s4 --color=never /var/log > logs-merged.log

Super Speedy Syslog Searcher will sort varying log messages by datetime.

JamesThomasMoon
  • 6,169
  • 7
  • 37
  • 63
-1

Insert all of your files to an empty Notepad++ document.

Edit → Line Operations → Sort Lines Lexicographically Ascending.

betontalpfa
  • 3,454
  • 1
  • 33
  • 65