Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of finding related logs in Linux

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "the method of Linux finding related logs". In the daily operation, I believe that many people have doubts about the method of finding related logs in Linux. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to find related logs in Linux". Next, please follow the editor to study!

1. Query string command: cat fileName | grep 'string to find'

Example: cat catalina.out | grep '2018041417434200258001'

two。 Query the text information before and after a string based on a string command: grep-A | B n "key" file

Where:

A: indicates after context after the string

B: indicates before context before the string

N: how many lines of text line number to get

Key: for the string to find

File: file name

Example: grep-A 100-B 100-I 'can't catch me' catalina.out,-i means ignoring case.

Of course, if we want to get the first 10 lines and the last 10 lines of the exception log, instead of adding-An and-B, we can use the following command:

Example: grep-10-I 'can't catch me' catalina.out

The 3.less command is used with grep

When there is a problem online, we often use various commands such as tail-n or tail-f or grep or vi cat to view the exception information.

But the log is constantly brushing the screen, and the tail is changing dynamically. We often expect to turn the page from the last line of the log to the previous page to find abnormal information, and I think the less command is the most practical and convenient.

If we want to look forward from the last line of the log in a log file, we can use the less command

Step 1: open the log file less sigma.log

Step 2: navigate to the last line of the log file: shift+g move to the last line

Step 3: ctrl+b turn the page to see the previous page.

Reference:

1. Full screen Navigation ctrl + F-move one screen forward ctrl + B-move back one screen ctrl + D-move half screen forward ctrl + U-move half screen backward 2. One-line navigation j-move forward one line k-move backward one line 3. Other navigation G-move to the last line g-move to the first line Q / ZZ-exit the less command

Common methods of use

1. Search when you use the command less file-name to open a file, you can use the following way to search in the file. Matching parts of the entire text are highlighted when searching.

1.1 search forward /: search using one pattern and navigate to the next matching text n: find the next matching text forward N: find the previous matching text backward 1.2 search?: use the pattern to search and navigate to the previous matching text n: find the next matching text N: find the previous matching text forward

2. Full-screen navigation

Ctrl + F: move one screen forward ctrl + B: move back one screen ctrl + D: move half screen forward ctrl + U: move half screen backward

3. One-way navigation

J: move down one line k: move one line up

4. Other navigation

G: move to the last line g: move to the first line press space: turn down one page b: turn up one page d: turn half a page down u: turn half a page up Q / ZZ: exit the less command

5. Edit the file

V: enter edit mode and edit the current file using the configured editor

6. Mark navigation

When you use less to view large files, you can mark them anywhere, and you can navigate to a text location marked with a specific tag through the command.

Ma: use a to mark the current position of the text

'a: navigate to mark a

7. Browse multiple files

In the first way, you can browse multiple files by passing multiple parameters to less.

Less file1 file2

Second, when browsing a file, use: e to open another file.

Less file1

: e file2

When opening multiple files, use the following command to switch between multiple files

: n-browse the next file

: P-browse the previous file

8. Less version tail-f

In Linux dynamic view log files commonly used command is tail-f, in fact, less can also do this job, using the F command. Use less file-name to open the log file and execute the command F to achieve an effect similar to tail-f.

Grep is used with less

Use grep filtering to complete and then use less to operate

Example: grep '84522ccf624edbab' tst-qrdish-web-api-*.log A10 | less

After grep filters the information before and after 10 lines of 84522ccf624edbab, then use less to operate

At this point, the study of "Linux's method of finding related logs" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report