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

Exploring the Linux Kernel: The Secrets of Kconfig

Get a deep understanding of how the Linux configu...

Virtual Box tutorial diagram of duplicating virtual machines

After getting used to VM, switching to BOX is a l...

MySQL sorting feature details

Table of contents 1. Problem scenario 2. Cause An...

Implementation of tomcat image created with dockerfile based on alpine

1. Download the alpine image [root@docker43 ~]# d...

mysql5.6.8 source code installation process

Kernel: [root@opop ~]# cat /etc/centos-release Ce...

Implementation of Docker to build Zookeeper&Kafka cluster

I've been learning Kafka recently. When I was...

Docker installation steps for Redmine

Download the image (optional step, if omitted, it...

How to monitor oracle database using zabbix agent2

Overview In zabbix version 5.0 and above, a new f...

Explanation of the usage of replace and replace into in MySQL

MySQL replace and replace into are both frequentl...

Introduction and examples of hidden fields in HTML

Basic syntax: <input type="hidden" na...

Detailed explanation of Angular component projection

Table of contents Overview 1. Simple Example 1. U...

Ten useful and simple MySQL functions

function 0. Display current time Command: select ...

Practice of using SuperMap in Vue

Table of contents Preface Related Materials Vue p...

How to implement the jQuery carousel function

This article shares the implementation code of jQ...