当前位置:网站首页>shell脚本详细介绍(四)
shell脚本详细介绍(四)
2022-06-26 12:40:00 【C和弦~】
目录
一.shell数组
1.数组的定义方法
格式:数组名=(元素1 元素2 元素3.......元素n)
2、数组包括的数据类型
- 数值类型
- 字符类型(字符串):使用" "或’ '定义,防止元素当中有空格,元素按空格分割
3.获取数组长度
4、获取数据列表
5、读取某索引赋值
6、数组遍历
7、数组切片
8、数组替换
9.数组删除
二.正则表达式
1.介绍
又称正规表达式、常规表达式,匹配一系列符合某个规则的字符串
- 基础正则表达式:BRE
- 扩展正则表达式:ERE
组成:普通字符和元字符
普通字符包括:大小写字母、数字、标点符号以及一些其他符号
元字符:在正则表达中具有特殊意义的专用字符,可以规定其前导字符(即位于元字符前面的字符)在目标对象中出现模式
2.基础正则表达式常见元字符
支持grep、egrep、sed、awk
常见元字符 解释
\ 转义字符,去除其后紧跟的元字符或通配符的特殊意义,例: !、\n、$等
^ 匹配字符串开始的位置,除非在方括号表达式中使用,表示不包含该字符集合。要匹配“^” 字符本身,请使用“^”
$ 匹配字符串结束的位置,如果设置了RegExp 对象的 Multiline 属性,则“KaTeX parse error: Undefined control sequence: \n at position 6: ”也匹配‘\̲n̲’或‘\r’。要匹配“”字符本身,请使用“$”
. 匹配除\n之外的任意的一一个字符,例:go.d、g…d
* 匹配前面子表达式0次或者多次,要匹配“”字符,请使用“*”,例: goo*d、 go.*d
[list] 匹配list列表中的一个字符,例:go [ola]d, [abc]、[a-z]、[a-z0-9]、[0-9]匹配任意一位数字
[^list] 匹配任意非list列表中的-一个字符,例: [^0-9]、 [^A-20-9]、 [^a-z],匹配任意一位非小写字母
[n1-n2] 字符范围。匹配指定范围内的任意一个字符。例如,“[a-z]”可以匹配“a”到“z”范围内的任意一个小写字母字符。
注意:只有连字符(-)在字符组内部,并且出现在两个字符之间时,才能表示字符的范围;如果出现在字符组的开头,则只能表示连字符本身
{n} n 是一个非负整数,匹配确定的 n 次。例如,“o{2}”不能匹配“Bob”中的“o”,但是能匹配“food”中的“oo”
{n,} n 是一个非负整数,至少匹配 n 次。例如,“o{2,}”不能匹配“Bob”中的“o”,但能匹配“foooood”中的所有o。“o{1,}”等价于“o+”。“o{0,}”则等价于“o*”
{n,m} m 和 n 均为非负整数,其中 n<=m,最少匹配 n 次且最多匹配m 次
注意 egrep, awk使用{n}、{n,}、{n,m}匹配时“{}"前不用加“\”
3.扩展正则表达式元字符
支持的工具:egerp、awk
元字符 解释
+ 匹配前面子表达式1次以上,例:go+d,将匹配至少一个o,如god, good, goood等
? 匹配前面子表达式0次或者1次,例: go?d,将匹配gd或god
() 将括号中的字符串作为一个整体,例:g(oo) +d,将匹配。整体1次以上,如good,gooood等
| 以或的方式匹配字条串,例:g(oo|la)d,将匹配good或者glad
+ egrep -n 'wo+d' test.txt #查询"wood" "woood" "woooooood"等字符串
? egrep -n 'bes?t' test.txt #查询“bet”“best”这两个字符串
| egrep -n 'of|is|on' test.tx #查询"of"或者"if"或者"on"字符串
() egrep -n 't(a|e)st' test.txt #查询"tast"或者"test"字符串
()+ egrep -n 'A(xyz)+C' test.txt #查询开头的"A"结尾是"C",中间有一个以上的"xyz"字符串的意思
4.grep工具
grep -c "the" web.sh #统计the字符总行数;
grep -i "the" web.sh #不区分大小写查找the所有的行
grep -v "the" web.sh #反选(除the以外的)
grep -n ".$" web.sh #顺便输出以.结尾的行号
4.1查找特定字符
grep -ni 'the' test.txt
#查找特定字符非常简单,如执行此命令即可从 test.txt 文件中查找出特定字符“the”所在位置。其中“-n”表示显示行号、“-i”表示不区分大小写。命令执行后,符合匹配标准的字符, 字体颜色会变为红色(本章中全部通过加粗显示代替)
grep -vn 'the' test.txt
#若反向选择,如查找不包含“the”字符的行,则需要通过 grep 命令的“-v”选项实现,并配合“-n”一起使用显示行号
4.2查找任意一个字符“.”与重复字符“*”
[[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查找行首“^”与行尾字符“$”
[[email protected] ~]# grep -n '^the' test.txt
4:the tongue is boneless but it breaks bones.12!
#基础正则表达式包含两个定位元字符:“^”(行首)与“$”(行尾),如果想要查询以“the”字符串为行首的行,则可以通过“^”元字符来实现。
[[email protected] ~]# grep -n '^$' test.txt
10:
#查询空白行
4.4查找连续字符范围
案例1:查询两个o的字符
[[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!
案例2:查询以 w 开头以 d 结尾,中间包含 2~5 个 o 的字符串
[[email protected] ~]# grep -n 'wo\{2,5\}d' test.txt
8:a wood cross! 11:#woood #
边栏推荐
- Decorator
- [how to connect the network] Chapter 1: the browser generates messages
- mysql讲解(一)
- J - Wooden Sticks poj 1065
- C - Common Subsequence
- Beifu PLC obtains system time, local time, current time zone and system time zone conversion through program
- What should the software test report include? Interview must ask
- Prototype
- Processsing mouse interactive learning
- Beifu twincat3 can read and write CSV and txt files
猜你喜欢
Electron official docs series: Get Started
Machine learning notes - seasonality of time series
外观模式(Facade)
ES6 module
Arcpy -- use of insertlayer() function: adding layers to map documents
P2393 yyy loves Maths II
Beifu realizes the control of time slice size and quantity through CTU and ton
Es6: iterator
Processsing function random
MySQL数据库常见故障——遗忘数据库密码
随机推荐
G - Cow Bowling
10秒内完成火灾预警,百度智能云助力昆明官渡打造智慧城市新标杆
C - Common Subsequence
Processing random generation line animation
P2393 yyy loves Maths II
Mysql database explanation (III)
Bifu divides EtherCAT module into multiple synchronization units for operation -- use of sync units
5+api, clear application cache
zoopeeper设置acl权限控制(只允许特定ip访问,加强安全)
Electron official docs series: Processes in Electron
解中小企业之困,百度智能云打个样
J - Wooden Sticks poj 1065
MySQL数据库讲解(三)
G - Cow Bowling
Mode pont
Here Document免交互及Expect自动化交互
I have a good word to say, and I admire myself
Solutions to insufficient display permissions of find and Du -sh
【Spark】. Explanation of several icons of scala file in idea
Uva11582 [fast power]colossal Fibonacci numbers!