Shell字符串处理

Shell 2019-03-10 3948 字 1240 浏览 点赞

前言

此系列为慕课网《跟着360架构师 学习Shell脚本编程》的学习笔记,与菜鸟教程中的 Shell 教程 搭配食用效果更佳。

删除与替换

语法说明

语法说明
${变量名#匹配规则}从变量开头匹配,将符合最短的数据删除
${变量名##匹配规则}从变量开头匹配,将符合最长的数据删除
${变量名%匹配规则}从变量尾部匹配,将符合最短的数据删除
${变量名%%匹配规则}从变量尾部匹配,将符合最长的数据删除
${变量名/旧字符串/新字符串}变量内容符合旧字符串规则,让第一个旧字符串被新字符串替代
${变量名//旧字符串/新字符串}变量内容符合旧字符串规则,让全部的旧字符串被新字符串替代

[#]

root@localhost:01# str="hello world"

root@localhost:01# echo ${str#wor}
# 说明:
# 类似Python中的re.match(),必须从第一个字符开始挨个匹配,如果途遇不能匹配字符,
# 那么匹配失败,所以删除空。也可认为原字符串什么都没删除。
hello world

root@localhost:01# echo ${str#hel*}  
# 说明:
# `*`可以匹配任意字符,
# 一个`#`表示关闭贪婪,尽量少的匹配,所以只有`hel`被删除
lo world

root@localhost:01# echo ${str##hel*}  
# 说明:
# 两个`#`表示开启贪婪,尽可能多的匹配,字符串全被删除,所以输出空

root@localhost:01# ...

[%]

#类似,不过前者从字符串头开始匹配,%表示从字符串尾开始匹配。匹配结束之后都要做删除操作。

root@localhost:01# str="hello world"

root@localhost:01# echo ${str%rrld}
hello world

root@localhost:01# echo ${str%rld}
hello wo

root@localhost:01# echo ${str%*rld}
# 说明:
# 一个`%`表示关闭贪婪
hello wo

root@localhost:01# echo ${str%%*rld}
# 说明:
# 两个`%`表示开启贪婪

root@localhost:01# ...

[/]

可以认为一个/是非贪婪,两个//是贪婪;但更好的理解方式是前者看作Python中的re.search(),后者看作Python中的re.findall()

root@localhost:01# str="hello world"

root@localhost:01# echo ${str/l/-}
# 说明:
# 在`str`中寻找`l`,找到第一个就停止,
# 再用`-`替换`str`中的`l`
he-lo world

root@localhost:01# echo ${str//l/-}
# 说明:
# 找出`str`中所有`l`,再用`-`替换这些`l`
he--o wor-d

root@localhost:01# ...

字符串长

语法说明

语法说明
${#str}返回字符串长度
expr length "$str"如果str表示的字符串有空格,必须加双引号

示例

root@localhost:01# str="hello world"

root@localhost:01# echo ${#str}
11
root@localhost:01# expr length "$str"
11
root@localhost:01# strlen=${#str}
# 说明: `${#...}`允许直接赋值给变量
root@localhost:01# strlen=`expr length "$str"`
# 说明: `expr length`方式必须用反引号包起来,才可以赋值给变量

字符位置

语法说明

语法说明
expr index "$str" str2如果在str2中的字符也在str中,输出最近字符的位置
expr match "$str" str2从头开始匹配子字符串,输出匹配到的字符个数,匹配不到返回0
expr substr "$str" pos lenpos表示位置,意为:从pos开始,返回len个字符

expr index

expr index的处理方式是,将str2中所有字符拆分开,这些被拆分出来的字符拿到str中匹配,输出在str中、从左到右、最先出现的字符的位置。注意是位置,不是索引,一般认为:位置 = 索引 + 1。

因此下面的示例中,无论是lok还是kol,得到结果相同。

root@localhost:01# str="hello world"

root@localhost:01# expr index "$str" lok
3
root@localhost:01# expr index "$str" kol
3
root@localhost:01# expr index "$str" k
0  # 说明:如果不存在,输出0

expr match

表示从左到右、一个接一个的匹配字符,如果str2中所有字符得到正确匹配,输出str2的长度(也就是匹配的字符的个数);如果存在至少一个字符不能被正确匹配,输出0。

root@localhost:01# str="hello world"

root@localhost:01# expr match "$str" hell
4
root@localhost:01# expr match "$str" hellk
0  # `k`不能正确匹配,输出0
root@localhost:01# expr match "$str" "hello "
6  # 说明:如果想匹配原字符串的空格,可对str2用引号包起来

expr substr

截取字符串,pos表示从哪个位置的字符开始截取,len表示截取多少个字符。

root@localhost:01# str="hello world"

root@localhost:01# expr substr "$str" 2 6
ello w
root@localhost:01# expr substr "$str" 2 100
ello world  # 说明:len超过原字符串长度不会报错
root@localhost:01# expr substr "$str" 100 100
  # 说明:输出空

截取字符串

语法说明

语法说明
${str:start}start为索引值,表示从索引start开始匹配,直到字符串结束
${str:start:len}表示从索引start开始,匹配len个字符
${str: -start}字符串最后一个字符索引为-1,倒数第二个为-2,依次;注意冒号后面有空格
${str:(-start)}用括号“()”可不写冒号后面的空格

示例

${:}方式截取字符串类似Python中的切片,start表示索引值。

root@localhost:01# str="hello world"

root@localhost:01# echo ${str:4}
o world

root@localhost:01# echo ${str:4:4}
o wo

root@localhost:01# echo ${str: -3:2}
rl  # 说明:注意冒号后面的空格不能省略

root@localhost:01# echo ${str:(-3):2}
rl  # 说明:有括号之后就不需要空格了

感谢



本文由 Guan 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

还不快抢沙发

添加新评论