Goal :
View the log file, where all null pointer exceptions occur, and 10 lines before and after,
The first 10 lines are to see the details of the request (interface, parameters, etc.),
The last 10 lines are to view the code line number where the exception occurred and locate code bugs.
Order:
grep -rnw "java.lang.NullPointerException" house_error.log |cut -d ':' -f 1 |xargs -n1 -i expr {} + 10 |xargs -i awk '{if(NR>={}-16 && NR<={})print NR":"$0;if(NR=={}) print "\n\n" }' house_error.log
Command Explanation:
Command execution result :
In awk:
$0: Indicates the entire line;
$1: Indicates the first field (domain) after delimiter; $ 2: Indicates the second field (domain) after delimiter; Delimiter : The default is space, newline, \ Tab
awk's built-in constants
There are many system variables in awk. These system variables are often used when we write awk scripts. I will now list the frequently used system variables and give a brief description.
Variable name description example$0 | Current record content | awk '{print $0}' |
$1~$n | The contents of field 1 to field n of the current record are respectively saved | awk '{print $1, $2, $3}' |
FS | Field separator, default is space or Tab | awk 'BEGIN{FS=":"}{print $1,$3,$6}' /etc/passwd |
NF | Record the number of fields in the current record | awk '{print $0} END{printf("Total Field(s):%d\n", NF)}' 201509.log |
NO | The number of lines that have been read, counted from 1; in the case of multiple files, the value will continue to accumulate | awk '{print NR}' 201508.log 201509.log |
FNR | For the currently processed file, the number of lines that have been read; for multiple files, the value is the line number corresponding to each file alone | awk '{print FNR}' 201508.log 201509.log |
RS | the input record separator, defaults to a newline | awk 'BEGIN{RS=" "}{print FNR}' 201508.log |
OFS | The output field separator, the default is also a space | awk 'BEGIN{OFS="\t"}{print $1, $2, $3}' 201509.log |
ORS | The output record separator, defaults to a newline | It is rarely used in general, and no examples are given here. |
FILENAME | The name of the current input file | awk '{print FILENAME}' 201509.log |
Precautions:
In expr arithmetic operations, there must be a space on both sides of the operator
refer to:
https://www.tuicool.com/articles/2mqmYbe