Example method to find keywords and their preceding and following information in Linux logs

Example method to find keywords and their preceding and following information in Linux logs

In daily work, we often need to view logs. For example, we can view logs in real time through the tail command, or we can view log information through commands such as cat .

But now we are going to discuss how to filter out the content we want from the log by keywords. There are many ways to do this. Today we will mainly learn about the cat command.

Assume that there is a log file hrun.log , and the query keyword is "new user":

View logs by keyword
cat hrun.log | grep "new user"

View the last 10 lines of logs according to keywords
cat hrun.log | grep "new user" -A 10

View the first 10 log lines by keyword
cat hrun.log | grep "new user" -B 10

View the 10 lines of logs before and after the keyword and display the line numbers
cat -n hrun.log | grep "New User" -C 10

View the first 50 lines of the log
cat hrun.log | head -n 50

View the last 50 lines of the log and display the line number
cat -n hrun.log | tail -n 50

illustrate:

  • -A means after the keyword.
  • -B means before the keyword.
  • -C means before and after the keyword, Context

The above is all the knowledge points about finding keywords in Linux logs introduced this time. Thank you for your learning and support for 123WORDPRESS.COM.

<<:  Elegant practical record of introducing iconfont icon library into vue

>>:  Examples of optimistic locking and pessimistic locking in MySQL

Recommend

Detailed explanation of mysql record time-consuming sql example

mysql records time-consuming sql MySQL can record...

vue-amap installation and usage steps

I have previously shared the usage of asynchronou...

Record the whole process of MySQL master-slave configuration based on Linux

mysql master-slave configuration 1. Preparation H...

Can CSS be used like this? The art of whimsical gradients

In the previous article - The charm of one line o...

Steps to create a CentOS container through Docker

Table of contents Preface Create a bridge network...

CSS3 uses scale() and rotate() to achieve zooming and rotation

1. scale() method Zoom refers to "reducing&q...

In-depth explanation of InnoDB locks in MySQL technology

Table of contents Preface 1. What is a lock? 2. L...

WeChat applet uses the video player video component

This article example shares the specific code of ...

Are the value ranges of int(3) and int(10) the same in mysql

Table of contents Question: answer: Reality: Know...

Detailed explanation of putting common nginx commands into shell scripts

1. Create a folder to store nginx shell scripts /...

Nginx cache files and dynamic files automatic balancing configuration script

nginx Nginx (engine x) is a high-performance HTTP...

Teach you to connect to MySQL database using eclipse

Preface Since errors always occur, record the pro...

Echarts legend component properties and source code

The legend component is a commonly used component...