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 script programming (1) (basic regular expressions, grep)

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

Share

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

Regular expressions of shell script programming (1) 1. Preface

​ this article mainly deals with the main concepts of shell regular expressions and the grep command, one of the commonly used "three Musketeers".

II. Definition of regular expressions

​ regular expressions: or regular expressions, regular expressions. It uses a single string to describe and match a series of strings that conform to a certain syntactic rule, that is, to quickly find, delete and replace a specific string through some special symbols. A text pattern consisting of ordinary characters and metacharacters.

​ common characters: such as uppercase and lowercase letters, numbers, punctuation, and other symbols

​ metacharacter: a special character with a special meaning that can be used to specify the occurrence pattern of its leading character (that is, the character that precedes the metacharacter) in the target object.

​ of course these concepts are too abstract and boring, and the following examples may help you understand these abstract concepts.

Regular expressions include:

Basic regular expressions-- grep and sed support extended regular expressions-- egrep and awk support the role of triple regular expressions

System administrators are commonly used and are one of the necessary skills. It is helpful to quickly locate important information and solve related problems.

Fourth, regular expression example

The following details the role of grep command in regular expressions and the use of formatting methods with examples, which leads to the meaning of metacharacters contained in basic regular expressions.

Grep command

-NMY-display line number-iMuy-ignore case-VMY-reverse look up [root@lokott opt] # cat test.txt / / test text content he was short and fat.He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.the tongue is boneless but it breaks bones.12! Google is the best tools for search keyword.The year ahead will test our political establishment to the limit.PI=3.141592653589793238462643383249901429a wood cross!Actions speak louder than words#woood # # woooooood # AxyzxyzxyzxyzCI bet this place is really spooky late at night! Misfortunes never come alone/single.I shouldn't have lett so tast.

1) find specific characters

[root@lokott opt] # grep-n 'the' test.txt / / Show the line number to retrieve the line containing the 4:the tongue is boneless but it breaks bones.12: 5: google is the best tools for search keyword.6:The year ahead will test our political establishment to the limit. [root@lokott opt] # grep-ni' the' test.txt / / Show the line number Case-insensitive 3:The home of Football on BBC Sport online.4:the tongue is boneless but it breaks bones.12 nv 5: google is the best tools for search keyword.6:The year ahead will test our political establishment to the limit. [root@lokott opt] # grep-nv 'the' test.txt / / shows the line number, and retrieves the line 1:he was short and fat.2:He was wearing a blue polo shirt with black pants without the. 3:The home of Football on BBC Sport online.7:PI=3.1415926535897932384626433832499014298:a wood cross!9:Actions speak louder than words10:11:12:#woood # 13:#woooooood # 14:AxyzxyzxyzxyzC15:I bet this place is really spooky late at night! 16:Misfortunes never come alone/single.17:I shouldn't have lett so tast.

2) below, use brackets [] to find the collection characters

[root@lokott opt] # grep-n'shio] rt' test.txt / / retrieve the line 1:he was short and fat.2:He was wearing a blue polo shirt with black pants that contains shirt or short. [root@lokott opt] # grep-n 'oo' test.txt / / Retrieval of repetitive characters 3:The home of Football on BBC Sport online.5: google is the best tools for search keyword.8:a wood crossbar 12 oo' test.txt woood # 13:#woooooood # 15 oo' test.txt I bet this place is really spooky late at night! [root@lokott opt] # grep-n'[^ w] oo' test.txt / / retrieve the line where the leading character of the oo character is not w 3:The home of Football on BBC Sport online.5: google is the best tools for search keyword.12:#woood # / / what matches the following two o, the leading character is the first o So when the three o's are displayed, they are red 13:#woooooood # / / what matches here is the first to the sixth o15 13:#woooooood I bet this place is really spooky late at night! [root@lokott opt] # grep-n'[^ a Murz] oo' test.txt / / matches the string oo preceded by a line of non-lowercase letters and matches Foo3:The home of Football on BBC Sport online. [root@lokott opt] # grep-n'[0-9] 'test.txt / / matches the numeric character 4:the tongue is boneless but it breaks bones.12room7purpurPIQU3.141592653589793238462643383249901429

3) find the beginning of the line ^ and the character at the end of the line $

Retrieve the instance at the beginning of the line:

[root@lokott opt] # grep-n'^ the' test.txt / / retrieve the line starting with the 4:the tongue is boneless but it breaks bones.12! [root@lokott opt] # grep-n'^ [Amurz] 'test.txt / / retrieve the line beginning with lowercase letter 1:he was short and fat.4:the tongue is boneless but it breaks bones.1208the' test.txt! [root@ Lokott opt] # grep-n'^ [a-zA-Z] 'test.txt / / retrieve the line 1:he was short and fat.2:He was wearing a blue polo shirt with black pants that begins with a letter. 3:The home of Football on BBC Sport online.4:the tongue is boneless but it breaks bones.12 days 6 speak louder than words14:AxyzxyzxyzxyzC15:I bet this place is really spooky late at night the year ahead will test our political establishment to the limit.7:PI=3.1415926535897932384626433832499014298:a wood crossroads 9 actions speak louder than words14:AxyzxyzxyzxyzC15:I bet this place is really spooky late at night! 16:Misfortunes never come alone/single.17:I shouldn't have lett so tast. [root@lokott opt] # grep-n'^ [^ a-zA-Z] 'test.txt / / does not search It is line 5 that begins with a letter: google is the best tools for search keyword.12:#woood # 13:#woooooood #

Note:! The meaning of the position of ^ here is different. The representation outside the brackets begins with the content in parentheses, otherwise it is reversed by the content.

To put it simply, 16-word summary: outside the square brackets, beginning within the brackets, inside the brackets, and taking the inside out.

Retrieve the instance at the end of the line $:

[root@lokott opt] # grep-n'\. $'test.txt / / search to click "." The line at the end of 1:he was short and fat. 3:The home of Football on BBC Sport online.5: google is the best tools for search keyword.6:The year ahead will test our political establishment to the limit.16:Misfortunes never come alone/single.17:I shouldn't have lett so tast. [root@lokott opt] # grep-n'^ $'test.txt / / retrieves the empty line 10:11:

Attention:!!. " When you represent the meaning of a dot, you need to use "\" to escape, because the period. " Is also a metacharacter

4) find any character "." And the repeating character "*"

[root@lokott opt] # grep-n'w.. d 'test.txt / / retrieve lines 5 that can be any two characters between w and d: google is the best tools for search keyword.8:a wood crosslinks 9 speak louder than words actions [root@lokott opt] # grep-n' wo*d' test.txt / / retrieve lines 8root@lokott opt a between w and d for 0 or more times Wood crossroads 12V ooo*d' test.txt woood # 13:#woooooood # [root@lokott opt] # grep-n 'ooo*d' test.txt / / search the third o which appears 0 or more times. A wood crossroads 12V ooo*d' test.txt woood # 13:#woooooood # [root@lokott opt] # grep-n 'w.principd' test.txt / / retrieve the line 1:he was of the optional characters between w and d Short and fat.5: google is the best tools for search keyword.8:a wood crossroads 9 speak louder than words12:#woood actions speak louder than words12:#woood # 13:#woooooood # [root@lokott opt] # grep-n'[0-9] [0-9] * 'test.txt / / search the line where any number is located: 4:the tongue is boneless but it breaks bones.123141592653589793238462643383249901429

5) find continuous character range "{}"

The main purpose of ​ {} is to limit the repetition of strings within a range, for example, to find consecutive characters of 3-5 o, you need to use {}, but because it has a specific meaning in shell, you need to use the escape character "\" to convert "{}" characters into normal characters.

[root@lokott opt] # grep-n'o\ {2\} 'test.txt / / search the line with two consecutive characters "o" 3:The home of Football on BBC Sport online.5: google is the best tools for search keyword.8:a wood crossroads 12 3:The home of Football on BBC Sport online.5 wooden # 13:#woooooood # 15 3:The home of Football on BBC Sport online.5 I bet this place is really spooky late at night! [root@lokott opt] # grep-n'wo\ {2Med 5\} d 'test.txt / / search line 8Rank a wood crossroad 12lane woood # [root@lokott opt] # grep-n' wo\ {2,\} d 'test.txt / / search w beginning d end o more than twice line 8Runa wood crossroad 12V metacharacter summary

Summary of common metacharacters in basic regular expressions

^-- the above 16-character formula

$- matches the end position of the input string

.-matches any single character except "\ r\ n"

\-Mark the next character as a special character, literal character, backward reference, octal escape character.

*-match the preceding leading character 0 or more times

[]-A collection of characters. Matches any of the characters contained.

[^]-- A collection of assigned characters. 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, matching determined n times.

{n,}-- n is a non-negative integer, matching at least n times

{N1 and N2}-both N1 and N2 are non-negative integers, N1

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