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 the grep command in linux

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to use the grep command in linux, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

The usage of grep command in linux

As one of the three most commonly used awk,sed,grep processing tools in linux, it is necessary to master its usage.

First of all, let's talk about the common format of the grep command: grep [option] "Mode" [file]

There are three grep families: grep,egrep,fgrep.

Common options:

-E: turns on the regular expression of the Extend.

-I: ignore case (ignore case).

-v: on the contrary (invert), only prints no matches, and matches do not print.

-n: displays the line number

-w: the text to be matched can only be a word, not a part of the word. For example, if there is liker in the text, and I am only searching for like, you can use the-w option to avoid matching liker.

-c: shows how many lines are matched, rather than what is matched, note that if you use the-cv option at the same time, it shows how many lines are not matched.

-o: only the strings matched by the pattern are displayed.

-- color: highlights the matching content in color.

-A n: displays the line in which the matching string is located and the n lines following it, after

-B n: displays the line of the matched string and its first n lines, before

-C n: displays the line of the matched string and the n lines before and after it, context

Mode section:

1. Enter the string to match directly, which can be replaced by fgrep (fast grep) to improve the search speed. For example, I want to match the number of printf in the hello.c file: grep-c "printf" hello.c.

2. Use basic regular expressions. Let's talk about the use of basic regular expressions:

Matching characters:

. : any character.

[abc]: means to match a character, which must be one of the abc.

[a-zA-Z]: means to match a character, which must be one of the 52 letters Amurz or Amurz.

[^ 123]: matches a character that is all but 1, 2, 3.

For some commonly used character sets, the system defines:

[A-Za-z] is equivalent to [: alpha:]

[0-9] is equivalent to [: digit:]

[A-Za-z0-9] is equivalent to [: alnum:]

White space characters such as tab,space [[: space:]]

[Amurz] is equivalent to [[: upper:]]

[amurz] is equivalent to [: lower:]

Punctuation marks [[: punct:]]

Number of matches:

\ {mdirection n\}: matches the characters that appear before them at least m times and n times at most.

\?: match the content that appears in front of it 0 times or 1 time, which is equivalent to\ {0Phone1\}.

*: match the content that appears before it any number of times, which is equivalent to\ {0,\}, so ". *" expresses any character any time, that is, no matter what content all matches.

Position Anchor:

^: anchor the beginning of the line

$: anchor the end of the line. Tip: "^ $" is used to match blank lines.

\ b or\: anchor the suffix of a word. For example, "\ blike\ b" does not match alike and liker, only like

\ B: opposite to\ b.

Grouping and referencing:

\ (string\): make string as a whole convenient for later reference

\ 1: reference the content matched by the first left parenthesis and its corresponding right parenthesis.

\ 2: reference the content matched by the second left parenthesis and its corresponding right parenthesis.

\ n: reference the content matched by the nth left parenthesis and its corresponding right parenthesis.

3. Extended (Extend) regular expression (note that you want to use the extended regular expression with the-E option, or use egrep directly):

Matching characters: this part is the same as the basic regular expression

Number of matches:

*: same as basic regular expression

The basic regular expression is\?, and there is no\ here.

{mdirection n}: compared to the basic regular expression, there is no\.

+: matches the character in front of it at least once, equivalent to {1,}.

Position anchoring: same as the basic regular expression.

Grouping and referencing:

(string): compared to basic regular expressions, there is no\.

\ 1: the reference section is the same as the basic regular expression.

\ n: the reference part is the same as the basic regular expression.

Or:

A | b: matches an or b. Note that a refers to the whole on the left of |, and the same goes for b. For example, C | cat means C or cat, not Cat or cat. If you want to represent Cat or cat, you should write (C | c) at. Remember (string) is used not only for references but also for grouping.

Note 1: by default, regular expression matching works in greedy mode, which means it matches as long as possible. For example, if a line has the string abacb, if the search content is "a.expresb", it will directly match the abacb string, not just ab or acb.

Note 2: all regular characters, such as [, *, (, etc.), can be escaped using\ * if you want to search for * instead of interpreting * as repeating the previous character any number of times.

The following is an exercise to end this grep study:

Retrieve all IP in the network configuration file / etc/sysconfig/network-scripts/ifcfg-ens33

1. Retrieve the range of 0-255

2. IP is composed of 0-255digits.

3. Simplification

Thank you for reading this article carefully. I hope the article "how to use grep commands in linux" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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

Development

Wechat

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

12
Report