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

How to use grep in linux

2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly shows you "how to use grep in linux", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "how to use grep in linux" this article.

In linux, the grep command is used to find qualified strings in the file with the syntax "grep option pattern file"; this command can find the file that contains the specified template style, and if the content of the file matches the specified template style, it will display the column containing the template style.

The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

The Linux grep command is used to find strings that match the criteria in the file.

The grep directive is used to find files that contain the specified template style. If the contents of a file are found to match the specified template style, the default grep directive will display the column containing the template style. If no file name is specified, or if the file name given is -, the grep instruction reads the data from the standard input device.

Grammar

Grep [- abcEFGhHilLnqrsvVwxy] [- A] [- B] [- C] [- d] [- e] [- f] [--help] [template style] [file or directory.]

Parameters:

-an or-- text: don't ignore binary data.

-An or-- after-context=: displays the content after that line, in addition to the column that matches the template style.

-b or-- byte-offset: marks the number of the first character of the line before displaying the line that matches the style.

-B or-- before-context=: displays the contents before the line, in addition to the line that matches the style.

-c or-- count: calculates the number of columns that match the style.

-C or-- context= or -: displays the content before and after the line, in addition to the line that matches the style.

-d or-- directories=: you must use this parameter when you specify that you are looking for a directory instead of a file, otherwise the grep instruction will return information and stop the action.

-e or-- regexp=: specifies the string as the style for finding the contents of the file.

-E or-- extended-regexp: use the style as an extended regular expression.

-f or-- file=: specifies a rule file whose content contains one or more rule styles and lets grep find the content of the file that meets the rule criteria in the format of one rule style per line.

-F or-- fixed-regexp: a list that treats styles as fixed strings.

-G or-- basic-regexp: use styles as common representations.

-h or-- no-filename: do not indicate the name of the file to which the line belongs until the line that matches the style is displayed.

-H or-- with-filename: indicates the name of the file to which the line belongs before the line that matches the style is displayed.

-I or-- ignore-case: ignore the difference in character case.

-l or-- file-with-matches: lists the file names whose contents match the specified style.

-L or-- files-without-match: lists the file names whose contents do not match the specified style.

-n or-- line-number: marks the column number of the row before displaying the row that matches the style.

-o or-- only-matching: only the matching PATTERN section is displayed.

-Q or-- quiet or-- silent: no information is displayed.

-r or-- recursive: this parameter has the same effect as specifying the "- d recurse" parameter.

-s or-- no-messages: no error message is displayed.

-v or-- invert-match: displays all lines that do not contain matching text.

-V or-- version: displays version information.

-w or-- word-regexp: only columns that match the full word are displayed.

-x-- line-regexp: only the columns that match the entire column are displayed.

-y: this parameter has the same effect as specifying the "- I" parameter.

Examples are as follows:

In the current directory, look for the file with the suffix file that contains the test string, and print out the line of the string. At this point, you can use the following command:

Grep test * file

The results are as follows:

$grep test test* # find files prefixed with "test" that contain "test" strings testfile1:This a Linux testfile! # list lines containing test characters in testfile1 files testfile_2:This is a linux testfile! # list lines containing test characters in testfile_2 files testfile_2:Linux test # list lines containing test characters in testfile_2 files above are all the contents of the article "how to use grep in linux", thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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

Servers

Wechat

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

12
Report