######${#} , 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
######expr match $string $substring
在string的开头,匹配substring字符串,返回匹配的字符串的长度
[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
####${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
######字符串处理时,使用正则表达式,注意:冒号两边必须有空格,否则语法错误
剪裁字符串开头的子串:两种方式
[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
剪裁字符串结尾的子串:也是两种方式区别是在\(前面添加.*
[root@server100 shell]# echo $str 012345 my name is mah [root@server100 shell]# expr match "$str" '.*\(...\)' #.表示一个字符 mah [root@server100 shell]# expr "$str" : '.*\(...\)' mah
######删除子串:格式一种:${}
第一类:从开头开始删除
[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
第二类:从结尾开始删除
[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
#######替换字符串
[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
转载于:https://blog.51cto.com/muahao/1531648