bash中字符串的处理(转)
http://hi.baidu.com/lizhoubob/blog/item/d500923d8ddb8d02baa167b5.html
1.得到字符串长度 方法一: $echo ${#variable} 代码:
zhyfly: ~$ x="this is a test" $expr length "$variable" 代码:
zhyfly: ~$ x="this is a test" $expr "$variable" : ".*" code: 代码:
zhyfly: ~$ x="this is a test" 方法: $expr index "$variable" "substring" code: 代码:
zhyfly: ~$ x="this is a test" 3.得到字符串子字符串 方法一: $echo ${variable:position:length} 代码:
zhyfly: ~$ x="this is a test" $expr substr "$variable" startposition length 代码:
zhyfly: ~$ x="this is a test" 4.匹配正则表达式之匹配长度 方法: $expr match "$x" "string" 代码:
zhyfly: ~$ x="this is a test" 方法: $echo ${variable#startletter*endletter} # #表示掐头,因为键盘上#在$前面,一个表示最小匹配 $echo ${variable##tartletter*endletter} 两个表示最大匹配 $echo ${variable%startletter*endletter} # %表示去尾,因为键盘上%在$后面,一个表示最小匹配 $echo ${variable%%startletter*endletter} 两个表示最大匹配 代码:
zhyfly: ~$ x="this is a test" 6.字符(串)的替换 方法: $echo ${variable/oldletter/newletter} #替换一个 $echo ${variable//oldletter/newletter} #替换所有 代码:
zhyfly: ~$ x="this is a test" |