当前位置:网站首页>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 #
边栏推荐
- Zoomeeper sets ACL permission control (only specific IP access is allowed to enhance security)
- Use the script to crawl the beautiful sentences of the sentence fan website and store them locally (blessed are those who like to excerpt!)
- 5月产品升级观察站
- Chapter 01_ Installation and use of MySQL under Linux
- Beifu realizes the control of time slice size and quantity through CTU and ton
- H5 video automatic playback and circular playback
- MySQL数据库讲解(六)
- Appearance mode (facade)
- 组合模式(Composite )
- Beifu PLC based on NT_ Shutdown to realize automatic shutdown and restart of controller
猜你喜欢
![[how to connect the network] Chapter 2 (middle): sending a network packet](/img/5f/602c59d3a7ee1154c7410fb2fcfc41.png)
[how to connect the network] Chapter 2 (middle): sending a network packet

MySQL explanation (I)

Decorator

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

MariaDB study notes

Processing random generation line animation

Learning Processing Zoog

ES基于Snapshot(快照)的数据备份和还原

What features are added to Photoshop 2022 23.4.1? Do you know anything

awk工具
随机推荐
适配器模式(Adapter)
I - Dollar Dayz
8、【STM32】定时器(TIM)——中断、PWM、输入捕获实验(一文精通定时器)
7-1 n queen problem
Electron official docs series: Development
Mode pont
Beifu PLC model selection -- how to see whether the motor is a multi turn absolute value encoder or a single turn absolute value encoder
Electron official docs series: Get Started
J - Wooden Sticks poj 1065
MySQL数据库讲解(六)
Beifu twincat3 can read and write CSV and txt files
Update and download of Beifu EtherCAT XML description file
Ring queue PHP
Beifu PLC realizes zero point power-off hold of absolute value encoder -- use of bias
What should the software test report include? Interview must ask
【Spark】. Explanation of several icons of scala file in idea
A collection of common tools for making we media videos
Do you know the limitations of automated testing?
imagecopymerge
Learning Processing Zoog