当前位置:网站首页>View log common commands

View log common commands

2022-08-09 07:05:00 Anakin6174

1. Common commands for viewing logs
tail:
-n is to display the line number; it is equivalent to the nl command; examples are as follows:
tail -100f test.log Real-time monitoring of 100 lines of logs
tail-n 10 test.log Query the log of the last 10 lines at the end of the log;

 tail -n +10 test.log Query all logs after 10 lines;head:Contrary to tail, tail is how many lines of log to look after; an example is as follows:head -n 10 test.log Query the first 10 lines of logs in the log file;head -n -10 test.log Query the log file for all logs except the last 10 lines;cat:tac is the reverse order view, which is the reverse of the cat word; examples are as follows:cat -n test.log |grep "debug" query the log of keywords
  1. Application scenario 1: View by line number - filter out logs near keywords

    1) cat -n test.log |grep "debug" to get the line number of the key log

    p>

    2) cat -n test.log |tail -n +92|head -n 20 Select the middle line where the keyword is located. Then view the log of the first 10 lines and the last 10 lines of the keyword:

     tail -n +92 means query the log after 92 lineshead -n 20 means to check the first 20 records in the previous query results
  2. Scenario 2: Query logs by date

    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 logs printed in the log, otherwise they are invalid;

     grep '2014-12-17 first16:17:20' test.log to determine if this time point is in the log

4. Application scenario 3: The log content is very large, and it is inconvenient to print it on the screen
(1) Use the more and less commands,

 Such as: cat -n test.log |grep "debug" |more This will print in pages, turn pages by clicking the space bar(2) Use >xxx.txt to save it to a file, then you can pull down this file for analysisSuch as: cat -n test.log |grep "debug" >debug.txt
  1. Scenario
    There is a large log file of dozens of meters, and the records in it are sorted by time.Now need to find an error message in it, I don't know where.At this time, it is difficult to copy the content. Even if it is copied, it is difficult for a general editor to hold such a large file.At this time, using less can solve this problem very easily and conveniently.

Using
The first is to open the file: less fileName.log

ctrl + f, look at the time, and turn to the next page.

If you feel that the information is too low, you can skip to the last line of the log file: shift+g

Then, ctrl+b to go to the previous page to view page by page

If you know the keywords, you can still search, press '/' and enter the words you want to search, and all the keywords will be highlighted.It is very convenient to find.

原网站

版权声明
本文为[Anakin6174]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208090655473502.html