Grep awk search log common commands

Time:2021-7-26

0 grep common parameters

reference resources:GNU Grep 3.0

--Color: highlight the matching string
-v: Display that cannot be matched by pattern
-i: Ignore character case
-o: Show only matching strings
-q: Silent mode, no information output
-A #: after, the last # line matched
-B #: before, the first # line matched
-C #: context, matching the front and rear # lines
-E: Using ere, extended regular expressions are supported

- C: output only the count of matching lines.
- I: case insensitive (only applicable to single character).
- H: when querying multiple files, the file name will not be displayed.
- L: when querying multiple files, only the file name containing matching characters will be output.
- N: display the matching line and line number.
-M: stop searching after matching how many keywords
- s: no error message with no or no matching text will be displayed.
- V: display all lines without matching text.

1 General: search for logs with error field in trace.log

grep ERROR trace.log

2 output file: the log can be output to a file

grep ERROR trace.log > error.log

3 reverse: search for logs that do not contain the error field

grep -v ERROR trace.log

4 forward: search for logs containing error and display the first 10 lines of error

grep -B 10 ERROR trace.log

5 backward: search the log that contains the error field and displays the last 10 lines of error

grep -A 10 ERROR trace.log

6 context: search for logs that contain the error field and display 10 lines before and after the error field

grep -C 10 ERROR trace.log

7 multi field: search for logs containing error and debug fields

gerp -E 'ERROR|DEBUG' trace.log

8 multiple files: search for logs containing error from multiple. Log files

grep ERROR *.log

9 omit file name: search for the log of error field from multiple. Log files, and the log file name is not displayed

Logs searched from multiple files have log file names on each line by default

grep -h ERROR *.log

10 time range: search logs by time range

awk '$2>"17:30:00" && $2<"18:00:00"' trace.log
The log format is as follows, $2 represents the second column, i.e. 11:44:58. Awk needs to specify the column

11-21 16:44:58 /user/info/

11 yes or No: stop searching after the first matching line is found

grep -m 1 ERROR trace.log

Recommended Today

Programming Xiaobai must understand the network principle

How is the network composed? Why can we easily surf the Internet now?Whether you are a computer major or not, you may always have such questions in your heart!And today we will solve this matter and tell you the real answer! Basic composition of network First, let’s look at this sentence Connect all computers together […]