1 View the first n lines of the log:
cat 或者 tail 日志文件名 | head -n 数量
示例1 :cat api.log | head -n 200 # 查看log前200行
示例2: tail api.log | head -n 10 # 查看log前10行
2 View the last n lines of the log:
cat 或者 tail 日志文件名 | tail -n 数量
示例: cat api.log | tail -n 200 # 查看log后200 行
示例: tail api.log | tail -n 15 # 查看log后15 行
3 Search the log according to the keyword and return all the lines where the keyword is located:
3.1 Method 1:
cat 或者 tail 日志文件名 | grep "关键词"
示例1:cat api.log | grep "前端入参" # 返回log日志中包含“前端入参”字样的所有行
示例2:tail api.log | grep "前端入参" # 返回log日志中包含“前端入参”字样的所有行
3.2 Method 2:
grep -i "关键词" 日志文件名
示例1:grep -i "21DYN60587" catalina.out # 返回log日志中包含 21DYN60587 的所有行
Common parameters:
parameter | illustrate |
---|---|
-f | Display the latest appended content of the file |
-q | When there are multiple file parameters, do not output individual file names |
-v | When there are multiple file parameters, always output the individual file names |
-c [number of bytes] | Display the last n bytes of the file |
-n [number of lines] | Display the content of the last n lines of the file |
Common parameters:
parameter | illustrate |
---|---|
-n or –number | show line number |
-b or --number-nonblank | Display line numbers, but do not number blank lines |
-s or --squeeze-blank | When there are more than two consecutive blank lines, only one blank line will be displayed\ |