SoFunction
Updated on 2025-03-09

Linux intercepts logs for a certain period of time

Linux intercepts logs for a certain period of time

Problem: On the server, the UAT environment logs log has 2G. It is obviously not appropriate to want to view and download it, so I want to intercept the logs for a certain period of time.

Because the exact start time and end time of the log are not clear, in order to avoid the interception failure due to the lack of corresponding time in the intercepted log, the log is intercepted using the regular expression [0-9].

The time format of the logs seized this time is:

[2023-01-09 23:10:00.001] [myScheduler-5]
[2023-01-09 23:10:00.001] [myScheduler-5]

Want to intercept data from 23 points from 2023-01-09 to 3 points from 2023-01-10

Note that the above time format is followed by a decimal point plus three digits. If the time format of the log is inconsistent with mine, it will cause the log interception failure, depending on your format.

1. The time format of the log is followed by a decimal point plus three digits.

sed -n '/2023-01-09 23:[0-5][0-9]:[0-5][0-9].[0-9][0-9][0-9]/,/2023-01-10 03:[0-5][0-9]:[0-5][0-9].[0-9][0-9][0-9]/p' Intercepted file name > The output file name

2. Log time format Normal time format intercept command

sed -n '/2023-01-09 23:[0-5][0-9]:[0-5][0-9]/,/2023-01-10 03:[0-5][0-9]:[0-5][0-9]/p' Intercepted file name > The output file name

ps: Linux intercepts logs within a certain time period

Preface: Intercept logs from 8 o'clock to 9 o'clock, because the exact start time and end time of the log are not clear, in order to avoid the interception failure due to the lack of corresponding time in the intercepted log, the log is intercepted using the regular expression [0-9].

The time format of the logs seized this time is:

2022-10-01 09:30:03.071

Note that the above time format is followed by a decimal point plus three digits. The time format of the log is inconsistent with mine, which will cause the log interception failure.

1. The time format of the log is followed by a decimal point plus three digits.

sed -n '/2022-10-01 11:[0-5][0-9]:[0-5][0-9].[0-9][0-9][0-9]/,/2022-10-01 12:[0-5][0-9]:[0-5][0-9].[0-9][0-9][0-9]/p' Intercepted file name.log > The output file name.log

2. Log time format Normal time format intercept command

sed -n '/2022-10-01 08:[0-9][0-9]:[0-9][0-9]/,/2022-10-01 09:[0-9][0-9]:[0-9][0-9]/p' Intercepted file name.log > The output file name.log

3. Use the following command to view the size of all files in the current folder (you can check whether the log has been intercepted successfully. If it is not successful, the log size of the intercepted log is almost the same as the original one)

ll -h

4. Use the following command to compress the logs to facilitate exporting the logs

tar -zcvf The compressed file name. The compressed file name.log

This is the end of this article about Linux intercepting logs for a certain period of time. For more related Linux intercepting logs for a certain period of time, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!