Bill Chang Bill Chang - 21 days ago 6
Bash Question

How to merge log files and sort by time

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

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 !

Answer

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.