参考文章:http://8186010.blog.51cto.com/8176010/1531648
######${#} , expr length "$str"
[root@server100 shell]
# str="my name is MAH"
[root@server100 shell]
# echo ${#str}
14
[root@server100 shell]
# expr length $str
expr
: syntax error
[root@server100 shell]
# expr length "$str" #必须要加双引号
14
########expr index "$str" MAH
[root@server100 shell]
# str="my name is MAH"
[root@server100 shell]
# expr index "$str" MAH
12
[root@server100 shell]
# expr index "$str" o #没有出现,返回0
0
[root@server100 shell]
# expr index "$str" Ay #尽管Ay都存在,但是返回的是y的位置
2
[root@server100 shell]
# expr index "$str" an
4
|
1
2
3
4
5
6
7
8
9
|
[root@server100 shell]
# str="my name is MAH"
[root@server100 shell]
# expr match "$str" my*
2
[root@server100 shell]
# expr match "$str" MAH #尽管字符串中包含MAH,但是MAH不在开头
0
[root@server100 shell]
# expr match "$str" m*
1
[root@server100 shell]
# expr match "$str" m.* #正则表达式 .* 表示所有
14
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
####${str:position:length}
字符串剪裁,两种方式
[root@server100 shell]
# str="abc def ghi"
[root@server100 shell]
# echo ${str:0}
abc def ghi
[root@server100 shell]
# echo ${str:0:3} #0表示第一位,3表示长度
abc
[root@server100 shell]
# echo ${str:0:5}
abc d
[root@server100 shell]
# echo ${str:-3} #-前面没有空格,显示所有字符串
abc def ghi
You have new mail
in
/var/spool/mail/root
[root@server100 shell]
# echo $str
abc def ghi
[root@server100 shell]
# echo ${str: -3} #-前有空格,从右边剪裁
ghi
[root@server100 shell]
# echo ${str: -6}
ef ghi
[root@server100 shell]
# expr substr "$str" 1 3 #expr substr "$str"格式,首位是1号位,后面必须有两个参数
abc
[root@server100 shell]
# expr substr "$str" 1 5
abc d
|
1
2
3
4
5
|
[root@server100 shell]
# str="012345 my name is mah"
[root@server100 shell]
# expr match "$str" '\([0-9]*\)' #方式一,使用match
012345
[root@server100 shell]
# expr "$str" : '\([0-9]*\)' #方式二,使用冒号
012345
|
1
2
3
4
5
6
|
[root@server100 shell]
# echo $str
012345 my name is mah
[root@server100 shell]
# expr match "$str" '.*\(...\)' #.表示一个字符
mah
[root@server100 shell]
# expr "$str" : '.*\(...\)'
mah
|
1
2
3
4
5
6
7
8
9
10
|
[root@server100 shell]
# echo $str
012345 my name is mah
[root@server100 shell]
# echo ${str#0123}
45 my name is mah
[root@server100 shell]
# echo ${str#345}
012345 my name is mah
[root@server100 shell]
# echo ${str#0*5} #这里*不是正则表达式,仅仅代表从0到5中所有字符
my name is mah
[root@server100 shell]
# echo ${str##0*5} # ##贪婪模式
my name is mah
|
1
2
3
4
5
6
|
[root@server100 shell]
# echo ${str%mah}
012345 my name is
[root@server100 shell]
# echo ${str%%m*h}
012345
[root@server100 shell]
# echo ${str%%m.*h} #由此可见,*不是正则表示式
012345 my name is mah
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
[root@server100 shell]
# str="0011 my name is mah 0011 "
[root@server100 shell]
# echo ${str/0*1/yyy}
yyy
[root@server100 shell]
# echo ${str/0**/yyy}
yyy
[root@server100 shell]
# echo ${str/0*0/yyy} #*号不是正则表达式,但是表示的是所有的字符
yyy11
[root@server100 shell]
# echo ${str//0*0/yyy}
yyy11
[root@server100 shell]
# echo ${str/0*m/yyy}
yyyah 0011
另外两种特殊的替换
#1.从开头替换
[root@server100 shell]
# echo ${str/#0011/MAH}
MAH my name is mah 0011
#2.从结尾替换
[root@server100 shell]
# echo ${str}
0011 my name is mah 0011
[root@server100 shell]
# echo ${str/%0011/MAH}
0011 my name is mah 0011
[root@server100 shell]
# str="123 my name is mah"
[root@server100 shell]
# echo ${str/%m*h/MAH}
123 MAH
|