当前位置:网站首页>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 #
边栏推荐
- Composite mode
- 7-2 a Fu the thief
- What are the common categories of software testing?
- [how to connect the network] Chapter 2 (Part 1): establish a connection, transmit data, and disconnect
- HDU 3709 Balanced Number
- Beifu cx5130 card replacement and transfer of existing authorization files
- 组合模式(Composite )
- Arcpy——InsertLayer()函数的使用:掺入图层到地图文档里
- B - Bridging signals
- Script - crawl the customized storage path of the cartoon and download it to the local
猜你喜欢
Vivado error code [drc pdcn-2721] resolution
MySQL数据库常见故障——遗忘数据库密码
Arcpy -- use of insertlayer() function: adding layers to map documents
8. [STM32] timer (TIM) -- interrupt, PWM, input capture experiment (proficient in timer)
Arcpy - - utilisation de la fonction insertlayer (): ajout de calques dans un document de carte
8、【STM32】定时器(TIM)——中断、PWM、输入捕获实验(一文精通定时器)
Arcpy——InsertLayer()函數的使用:摻入圖層到地圖文檔裏
IDC报告:百度智能云AI Cloud市场份额连续六次第一
2. Introduction to parallel interface, protocol and related chips (8080, 8060)
Stack, LIFO
随机推荐
Digital signal processing -- Design of linear phase type (Ⅰ, Ⅲ) FIR filter (1)
What should the software test report include? Interview must ask
Arcpy——InsertLayer()函数的使用:掺入图层到地图文档里
ES中索引别名(alias)的到底有什么用
Aesthetic experience (episode 238) Luo Guozheng
防火墙介绍
MySQL explanation (I)
awk工具
MySQL讲解(二)
Generate JDE dot train
Oplg: new generation cloud native observable best practices
计算两点之间的距离(二维、三维)
LeetCode_ Stack_ Medium_ 150. evaluation of inverse Polish expression
KITTI Tracking dataset whose format is letf_ top_ right_ bottom to JDE normalied xc_ yc_ w_ h
7-2 a Fu the thief
A few lines of code can realize complex excel import and export. This tool class is really powerful!
Analysis and protection of heart blood dripping vulnerability (cve-2014-0160)
2、并行接口、协议和相关芯片介绍(8080、8060)
C - Common Subsequence
Arcpy——InsertLayer()函數的使用:摻入圖層到地圖文檔裏