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

The HTML 5 draft did not become a formal standard

<br />Yesterday I saw at W3C that the new HT...

MySQL FAQ series: How to avoid a sudden increase in the size of the ibdata1 file

0. Introduction What is the ibdata1 file? ibdata1...

Native JS to implement sharing sidebar

This article shares a sharing sidebar implemented...

How to install ionCube extension using pagoda

1. First install the pagoda Installation requirem...

Vue3 list interface data display details

Table of contents 1. List interface display examp...

How to use resize to implement image switching preview function

Key Points The CSS resize property allows you to ...

Detailed explanation of how MySQL solves phantom reads

1. What is phantom reading? In a transaction, aft...

Introduction and use of js observer mode

Table of contents I. Definition 2. Usage scenario...

How to use nginx to access local static resources on Linux server

1. Check whether port 80 is occupied. Generally, ...

HTML elements (tags) and their usage

a : Indicates the starting or destination positio...

Solutions to the failure and invalidity of opening nginx.pid

Table of contents 1. Problem Description 2. Probl...

Detailed explanation of JS ES6 coding standards

Table of contents 1. Block scope 1.1. let replace...

Detailed explanation of nginx installation, deployment and usage on Linux

Table of contents 1. Download 2. Deployment 3. Ng...