当前位置:网站首页>Detailed introduction to shell script (4)

Detailed introduction to shell script (4)

2022-06-26 13:22:00 C chord~

Catalog

One .shell Array

1. How to define an array

 2、 The data types included in the array

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

2. Common metacharacters in basic regular expressions

3. Extended regular expression metacharacter

4.grep Tools

4.1 Find specific characters

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 #

原网站

版权声明
本文为[C chord~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261227386299.html