######${#} , expr length "$str" [[email protected] shell]# str="my name is MAH" [[email protected] shell]# echo ${#str} 14 [[email protected] shell]# expr length $str expr: syntax error [[email protected] shell]# expr length "$str" #必须要加双引号 14 ########expr index "$str" MAH [[email protected] shell]# str="my name is MAH" [[email protected] shell]# expr index "$str" MAH 12 [[email protected] shell]# expr index "$str" o #没有出现,返回0 0 [[email protected] shell]# expr index "$str" Ay #尽管Ay都存在,但是返回的是y的位置 2 [[email protected] shell]# expr index "$str" an 4
######expr match $string $substring
在string的开头,匹配substring字符串,返回匹配的字符串的长度
[[email protected] shell]# str="my name is MAH" [[email protected] shell]# expr match "$str" my* 2 [[email protected] shell]# expr match "$str" MAH #尽管字符串中包含MAH,但是MAH不在开头 0 [[email protected] shell]# expr match "$str" m* 1 [[email protected] shell]# expr match "$str" m.* #正则表达式 .* 表示所有 14
####${str:position:length} 字符串剪裁,两种方式 [[email protected] shell]# str="abc def ghi" [[email protected] shell]# echo ${str:0} abc def ghi [[email protected] shell]# echo ${str:0:3} #0表示第一位,3表示长度 abc [[email protected] shell]# echo ${str:0:5} abc d [[email protected] shell]# echo ${str:-3} #-前面没有空格,显示所有字符串 abc def ghi You have new mail in /var/spool/mail/root [[email protected] shell]# echo $str abc def ghi [[email protected] shell]# echo ${str: -3} #-前有空格,从右边剪裁 ghi [[email protected] shell]# echo ${str: -6} ef ghi [[email protected] shell]# expr substr "$str" 1 3 #expr substr "$str"格式,首位是1号位,后面必须有两个参数 abc [[email protected] shell]# expr substr "$str" 1 5 abc d
######字符串处理时,使用正则表达式,注意:冒号两边必须有空格,否则语法错误
剪裁字符串开头的子串:两种方式
[[email protected] shell]# str="012345 my name is mah" [[email protected] shell]# expr match "$str" ‘\([0-9]*\)‘ #方式一,使用match 012345 [[email protected] shell]# expr "$str" : ‘\([0-9]*\)‘ #方式二,使用冒号 012345
剪裁字符串结尾的子串:也是两种方式区别是在\(前面添加.*
[[email protected] shell]# echo $str 012345 my name is mah [[email protected] shell]# expr match "$str" ‘.*\(...\)‘ #.表示一个字符 mah [[email protected] shell]# expr "$str" : ‘.*\(...\)‘ mah
######删除子串:格式一种:${}
第一类:从开头开始删除
[[email protected] shell]# echo $str 012345 my name is mah [[email protected] shell]# echo ${str#0123} 45 my name is mah [[email protected] shell]# echo ${str#345} 012345 my name is mah [[email protected] shell]# echo ${str#0*5} #这里*不是正则表达式,仅仅代表从0到5中所有字符 my name is mah [[email protected] shell]# echo ${str##0*5} # ##贪婪模式 my name is mah
第二类:从结尾开始删除
[[email protected] shell]# echo ${str%mah} 012345 my name is [[email protected] shell]# echo ${str%%m*h} 012345 [[email protected] shell]# echo ${str%%m.*h} #由此可见,*不是正则表示式 012345 my name is mah
#######替换字符串
[[email protected] shell]# str="0011 my name is mah 0011 " [[email protected]0 shell]# echo ${str/0*1/yyy} yyy [[email protected] shell]# echo ${str/0**/yyy} yyy [[email protected] shell]# echo ${str/0*0/yyy} #*号不是正则表达式,但是表示的是所有的字符 yyy11 [[email protected] shell]# echo ${str//0*0/yyy} yyy11 [[email protected] shell]# echo ${str/0*m/yyy} yyyah 0011 另外两种特殊的替换 #1.从开头替换 [[email protected] shell]# echo ${str/#0011/MAH} MAH my name is mah 0011 #2.从结尾替换 [[email protected] shell]# echo ${str} 0011 my name is mah 0011 [[email protected] shell]# echo ${str/%0011/MAH} 0011 my name is mah 0011 [[email protected] shell]# str="123 my name is mah" [[email protected] shell]# echo ${str/%m*h/MAH} 123 MAH
字符串处理-${#},expr length,expr index,expr match,抽取子串
时间: 2024-10-20 04:15:58