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

HTML tags explained

HTML tags explained 1. HTML tags Tag: !DOCTYPE De...

W3C Tutorial (9): W3C XPath Activities

XPath is a language for selecting parts of XML do...

Solution to multiple 302 responses in nginx proxy (nginx Follow 302)

Proxying multiple 302s with proxy_intercept_error...

Eight hook functions in the Vue life cycle camera

Table of contents 1. beforeCreate and created fun...

JavaScript implements constellation query function with detailed code

Table of contents 1. Title 2. Code 3. Results IV....

VMware virtual machine to establish HTTP service steps analysis

1. Use xshell to connect to the virtual machine, ...

Detailed process of getting started with docker compose helloworld

Prerequisites Compose is a tool for orchestrating...

jQuery realizes the shuttle box function

This article example shares the specific code of ...

Input file custom button beautification (demo)

I have written such an article before, but I used...

Vue project realizes paging effect

The paging effect is implemented in the vue proje...

A useful mobile scrolling plugin BetterScroll

Table of contents Make scrolling smoother BetterS...

Example of how to create a local user in mysql and grant database permissions

Preface When you install MySQL, you usually creat...

What are the benefits of semantic HTML structure?

one: 1. Semantic tags are just HTML, there is no ...