Note: The log file is: Test.log
1.tail-f Test.log
View the currently printed log (this method is always known!) There is a limit to the length of the printout. )
The following are collected online:
2. You must first understand the two most basic commands:
-
tail -n 10 Test.log Log at the end of the last 10 lines of the query log;
Tail-n +10 test.log queries all logs after 10 rows;
Head-n 10 test.log query log file for the first 10 lines of the log;
Head-n -10 test.log query log file except for all other logs in the last 10 lines;
-
Scene 1: View---By line number filter out the log near the keyword
Because we usually get very few logs with grep, we need to look at the logs nearby.
I do this, first: cat-n test.log |grep "terrain" get the line number of the critical log
-
<3> the line number where the "terrain" keyword is located is 102 rows. At this point, if I want to see the logs for the first 10 rows and the last 10 lines of this keyword:
Cat- n test.log |tail-n +92|head-n
Tail-n +92 means that the log after 92 rows is queried
Head-n 20 indicates that the previous query results are checked for the first 20 records
-
Scenario 2: So what's the date?? usually we really need to find the log at the specified time.
Sed-n '/2014-12-17 16:17:20/,/ 2014-12-17 16:17:36/p ' Test.log
Special note: The above two dates must be the one printed in the log, otherwise invalid.
For date printing, you can first grep ' 2014-12-17 16:17:20 ' test.log to determine if there is a point in the log to ensure that the 4th step can get the log
This is a very useful command for querying logs based on time periods.
If we look for a lot of logs, printing on the screen is not easy to view, there are two ways:
(1) Use the more and less commands, such as: Cat-n test.log |grep "terrain" |more so that the page printing, by clicking the Space bar to page
(2) using >xxx.txt to save it to a file, you can pull down this file analysis. For example:
Cat-n test.log |grep "Terrain" >xxx.txt
Linux View Log Method summary (1)