Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge log files and sort by time

Tags:

shell

logging

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 !

like image 264
Bill Chang Avatar asked Nov 17 '16 10:11

Bill Chang


1 Answers

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.

like image 152
Ryota Avatar answered Nov 10 '22 04:11

Ryota