当前位置:网站首页>Detailed introduction to shell script (4)
Detailed introduction to shell script (4)
2022-06-26 13:22:00 【C chord~】
Catalog
2、 The data types included in the array
2. Common metacharacters in basic regular expressions
3. Extended regular expression metacharacter
4.2 Find any character “.” And repeating characters “*”
4.3 Find the beginning of a line “^” And end of line characters “$”
4.4 Find range of consecutive characters
One .shell Array
1. How to define an array
Format : Array name =( Elements 1 Elements 2 Elements 3....... Elements n)

2、 The data types included in the array
- value type
- Character type ( character string ): Use " " or ’ ' Definition , Prevent spaces in elements , Elements are separated by spaces
3. Get array length

4、 Get the data list

5、 Read an index assignment

6、 Array traversal


7、 Array slice

8、 Array substitution

9. Array delete

Two . Regular expressions
1. Introduce
Also called normal expression 、 Regular expressions , Match a series of strings that conform to a certain rule
- Basic regular expressions :BRE
- Extended regular expression :ERE
form : Ordinary characters and metacharacters
Common characters include : Case letters 、 Numbers 、 Punctuation and some other symbols
Metacharacters : Special characters with special meaning in regular expression , Its leading character can be specified ( The character before the metacharacter ) The pattern... Appears in the target object
2. Common metacharacters in basic regular expressions
Support grep、egrep、sed、awk
Common metacharacters explain
\ Escape character , Remove the special meaning of the following metacharacter or wildcard , example : !、\n、$ etc.
^ Match the beginning of the string , Unless used in a bracket expression , Indicates that the character set... Is not included . To match “^” Character itself , Please use “^”
$ Match the end of the string , If set RegExp Object's Multiline attribute , be “KaTeX parse error: Undefined control sequence: \n at position 6: ” Also match ‘\̲n̲’ or ‘\r’. To match “” Character itself , Please use “$”
. Matching elimination \n Any one character other than , example :go.d、g…d
* Match the front face expression 0 Times or more , To match “” character , Please use “*”, example : goo*d、 go.*d
[list] matching list A character in the list , example :go [ola]d, [abc]、[a-z]、[a-z0-9]、[0-9] Match any digit
[^list] Match any non list In the list - A character , example : [^0-9]、 [^A-20-9]、 [^a-z], Match any non lowercase letter
[n1-n2] character in range . Match any character in the specified range . for example ,“[a-z]” Can match “a” To “z” Any lowercase character in the range .
Be careful : Hyphens only (-) Inside character group , And when it appears between two characters , To represent the range of characters ; If it appears at the beginning of a character group , Only the hyphen itself
{n} n Is a non negative integer , Matched definite n Time . for example ,“o{2}” Can't match “Bob” Medium “o”, But it matches “food” Medium “oo”
{n,} n Is a non negative integer , Match at least n Time . for example ,“o{2,}” Can't match “Bob” Medium “o”, But it can match. “foooood” All in o.“o{1,}” Equivalent to “o+”.“o{0,}” Is equivalent to “o*”
{n,m} m and n All non negative integers , among n<=m, Least match n Times and at most m Time
Be careful egrep, awk Use {n}、{n,}、{n,m} When the match “{}" There is no need to add “\”
3. Extended regular expression metacharacter
Supported tools :egerp、awk
Metacharacters explain
+ Match the front face expression 1 More than once , example :go+d, Will match at least one o, Such as god, good, goood etc.
? Match the front face expression 0 Time or 1 Time , example : go?d, Will match gd or god
() Take the string in parentheses as a whole , example :g(oo) +d, Will match . whole 1 More than once , Such as good,gooood etc.
| Match a string of words by or , example :g(oo|la)d, Will match good perhaps glad
+ egrep -n 'wo+d' test.txt # Inquire about "wood" "woood" "woooooood" Etc
? egrep -n 'bes?t' test.txt # Inquire about “bet”“best” These two strings
| egrep -n 'of|is|on' test.tx # Inquire about "of" perhaps "if" perhaps "on" character string
() egrep -n 't(a|e)st' test.txt # Inquire about "tast" perhaps "test" character string
()+ egrep -n 'A(xyz)+C' test.txt # At the beginning of the query "A" It ends with "C", More than one in the middle "xyz" Meaning of string
4.grep Tools
grep -c "the" web.sh # Statistics the Total number of characters ;
grep -i "the" web.sh # Case insensitive lookup the All of the line
grep -v "the" web.sh # Reverse election ( except the Outside of the )
grep -n ".$" web.sh # Output incidentally with . The line number at the end
4.1 Find specific characters
grep -ni 'the' test.txt
# Finding specific characters is very simple , If you execute this command, you can start from test.txt Find specific characters in the file “the” The position of . among “-n” Indicates display line number 、“-i” Indicates case insensitive . After the execution of the command , Matching characters , The font color changes to red ( All in this chapter are shown in bold instead of )
grep -vn 'the' test.txt
# If reverse selection , If the search does not contain “the” Lines of characters , You have to go through grep Ordered “-v” Option implementation , And cooperate with “-n” Use together to display line numbers
4.2 Find any character “.” And repeating characters “*”
[[email protected] ~]# grep -n 'w..d' test.txt
5:google is the best tools for search keyword.
8:a wood cross!
9:Actions speak louder than words
4.3 Find the beginning of a line “^” And end of line characters “$”
[[email protected] ~]# grep -n '^the' test.txt
4:the tongue is boneless but it breaks bones.12!
# The base regular expression contains two positioning metacharacters :“^”( Head of line ) And “$”( At the end of the line ), If you want to query with “the” The line with the string at the beginning of the line , You can use the “^” Metacharacters .
[[email protected] ~]# grep -n '^$' test.txt
10:
# Query blank lines
4.4 Find range of consecutive characters
Case study 1: Check two o The characters of
[[email protected] ~]# grep -n 'o\{2\}' test.txt
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
8:a wood cross!
11:#woood # 12:#woooooood #
14:I bet this place is really spooky late at night!
Case study 2: Query to w Begin with d ending , The middle contains 2~5 individual o String
[[email protected] ~]# grep -n 'wo\{2,5\}d' test.txt
8:a wood cross! 11:#woood #
边栏推荐
- Coprime and non coprime of template problems of Chinese remainder theorem
- Design of four kinds of linear phase FIR filters -- complete set of Matlab source code
- Here Document免交互及Expect自动化交互
- LeetCode_ Stack_ Medium_ 150. evaluation of inverse Polish expression
- 8. [STM32] timer (TIM) -- interrupt, PWM, input capture experiment (proficient in timer)
- Design of simple digital circuit traffic light
- Prototype
- MediaPipe手势(Hands)
- Detailed practical sharing, two hours of funny videos after work, earning more than 7000 a month
- 原型模式(prototype)
猜你喜欢

ES6:Map

Processing polyhedron change

Beifu realizes the control of time slice size and quantity through CTU and ton

What are the common categories of software testing?

外观模式(Facade)

计算两点之间的距离(二维、三维)

Es6: iterator

Echart stack histogram: add white spacing effect setting between color blocks

NVM installation tutorial

Oplg: new generation cloud native observable best practices
随机推荐
[shell] generate strings between specified dates
首批通过!百度智能云曦灵平台获信通院数字人能力评测权威认证
享元模式(Flyweight)
7-16 monetary system I
scrapy——爬取漫画自定义存储路径下载到本地
Processing polyhedron change
Use the script to crawl the beautiful sentences of the sentence fan website and store them locally (blessed are those who like to excerpt!)
Electron official docs series: Distribution
Beifu cx5130 card replacement and transfer of existing authorization files
C - Common Subsequence
Aesthetic experience (episode 238) Luo Guozheng
Common faults of MySQL database - forgetting database password
Fire warning is completed within 10 seconds, and Baidu AI Cloud helps Kunming Guandu build a new benchmark of smart city
Mysql database explanation (6)
Thinking caused by the error < note: candidate expectations 1 argument, 0 provided >
Beifu PLC based on NT_ Shutdown to realize automatic shutdown and restart of controller
桥接模式(Bridge)
D - skiing
Composite mode
A collection of common tools for making we media videos