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

Regular expressions of shell scripts (1)-grep, egrep

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Definition of regular expression

Regular expressions are also known as regular expressions and regular expressions. A regular expression is a text pattern consisting of ordinary characters and metacharacters. Patterns are used to describe one or more strings to match when searching for text. Regular expressions are commonly used in scripting and text editors. Many text processors and programming languages support regular expressions, such as grep, egrep, sed, awk, which are common in Linux systems. Regular expression has a powerful function of text matching, which can process text quickly and efficiently in the ocean of text.

Regular expression usage

Regular expressions are very important for system administrators, and a large amount of information will be generated during the operation of the system, some of which are very important and some are just informed information. As a system administrator, if you look at so much information data directly, you can't quickly locate the important information, such as "user account login failure", "service startup failure" and so on. At this point, you can quickly extract "problematic" information through regular expressions. In this way, the operation and maintenance work can become more simple and convenient.

Basic regular expression-- grep

The string expression method of regular expression can be divided into basic regular expression and extended regular expression according to different degree of rigor and function. The underlying regular expression is the most basic part of a commonly used regular expression.

1. Find specific characters

-n indicates the display line number

-I means case-insensitive

(for characters that meet the matching criteria, the font color will change to red)

Example demonstration

(1) find out the location of the specific character "the"

[root@localhost ~] # grep-n 'the' / opt/httpd.conf

(2) reverse search for lines that do not contain the character "the"

[root@localhost~] # grep-vn 'the' / opt/httpd.conf

two。 Use brackets "[]" to find collection characters

(1) look for the strings "shirt" and "short". No matter how many characters there are in "[]", they all represent only one character, that is, "[io]" means to match "I" or "o".

[root@localhost] # grep-n'sh [io] rt' / opt/httpd.conf

(2) find characters that repeat a single word

[root@localhost ~] # grep-n 'oo' / opt/httpd.conf

(3) search for strings that are not preceded by "R" by selecting "[^]" in the reverse direction of the collection characters.

[root@localhost ~] # grep-n'[^ R] oo' / opt/httpd.conf

(4) find lowercase or uppercase letters before "oo", where "Amurz" represents lowercase letters and "Amurz" represents uppercase letters.

[root@localhost ~] # grep-n'[^ a Murz] oo' / opt/httpd.conf / / lowercase letters

[root@localhost] # grep-n'[^ Amurz] oo' / opt/httpd.conf / / uppercase letters

(5) find rows that contain numbers

[root@localhost ~] # grep-n'[0-9]'/ opt/httpd.conf

3. Find the beginning of the line "^" and the character "$" at the end of the line

(1) find lines that start with root

[root@localhost ~] # grep-n'^ root' / etc/passwd

(2) find lines ending in bash

[root@localhost ~] # grep-n 'bash$' / etc/passwd

(3) query lines that begin with lowercase or uppercase letters

Lines that begin with lowercase letters can be filtered by the "^ [a murz]" rule, queries that begin with uppercase letters use the "^ [A murz]" rule, and queries that do not begin with letters use the "^ [^ a-zA-Z]" rule.

[root@localhost ~] # grep-n'^ [a Murz]'/ etc/passwd

(4) query with question mark? At the end of the line, you need to use the escape character "\" to convert characters with special meaning into ordinary characters.

[root@localhost ~] # grep-n'\? $'/ opt/httpd.conf

(5) query a blank line and use ^ $

[root@localhost ~] # grep-n'^ $'/ opt/httpd.conf

4. Find any character "." And the repeating character "*"

(1) find strings that begin with w and end with d

[root@localhost] # grep-n'w.. d' / opt/httpd.conf

(2) the query contains at least two or more strings, using asterisk metacharacters. It represents the repetition of zero or more previous single characters, so all materials that contain o, oo, ooo, ooo, etc., are up to the standard.

[root@localhost ~] # grep-n 'ooo*' test.txt

(3) the query begins with w and ends with a string of at least one o

[root@localhost ~] # grep-n 'woo*d' test.txt

(4) A string in which the query begins with w and ends with d, and the characters in the middle are optional.

[root@localhost ~] # grep-n'. Progresd' test.txt

(5) query the line of any number

[root@localhost ~] # grep-n'[0-9] [0-9] *'/ opt/httpd.conf

5. Find contiguous character range {}

When using the "{}" character, you need to use the escape character "\" to convert the "{}" character into a normal character.

(1) query the characters of two o

[root@localhost ~] # grep-n'o\ {2\} 'test.txt

(2) the query begins with w and ends with d, and contains 2-3 o strings in the middle.

Root@localhost ~] # grep-n'wo\ {2,\} d'test.txt

(3) the query begins with w and ends with d, with strings of more than 2 o in the middle

[root@localhost ~] # grep-n'wo\ {2,\} d'test.txt

Basic regular expressions are common metacharacter metacharacters that act as ^ matches the beginning of the input string. Used in square brackets expressions to indicate that the character collection is not included. $matches the end of the input string. . Matches any single character except "\ r\ n" marks the next character as a special character, literal character, backward reference, octal escape character. * matches the previous subexpression zero or more times. To match the "" character, use the "\" [] character collection. Matches any of the characters contained. For example, "[abc]" can match the set of "a" ^] assigned characters in "plain". Matches any character that is not included. [n1-n2] character range. Matches any character in the specified range. {n} n is a non-negative integer, and the n times {n,} n determined by matching is a non-negative integer, at least n times. Both n and n are non-negative integers, where n

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