Linux Shell字符串操做(长度\查找\替换)详解

在作shell批处理程序时候,常常会涉及到字符串相关操做。有不少命令语句,如:awk,sed均可以作字符串各类操做。 其实shell内置一系列操做符号,能够达到相似效果,你们知道,使用内部操做符会省略启动外部程序等时间,所以速度会很是的快。web

1、判断读取字符串值正则表达式

表达式 含义
${var} 变量var的值, 与$var相同
${var-DEFAULT} 若是var没有被声明, 那么就以$DEFAULT做为其值 *
${var:-DEFAULT} 若是var没有被声明, 或者其值为空, 那么就以$DEFAULT做为其值 *
${var=DEFAULT} 若是var没有被声明, 那么就以$DEFAULT做为其值 *
${var:=DEFAULT} 若是var没有被声明, 或者其值为空, 那么就以$DEFAULT做为其值 *
${var+OTHER} 若是var声明了, 那么其值就是$OTHER, 不然就为null字符串
${var:+OTHER} 若是var被设置了, 那么其值就是$OTHER, 不然就为null字符串
${var?ERR_MSG} 若是var没被声明, 那么就打印$ERR_MSG *
${var:?ERR_MSG} 若是var没被设置, 那么就打印$ERR_MSG *
${!varprefix*} 匹配以前全部以varprefix开头进行声明的变量
${!varprefix@} 匹配以前全部以varprefix开头进行声明的变量

加入了“*” 不是意思是: 固然, 若是变量var已经被设置的话, 那么其值就是$var.shell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[chengmo<a href= "http://www.jobbole.com/members/localhost" >@localhost< /a > ~]$ echo ${abc- 'ok' }
ok
[chengmo<a href= "http://www.jobbole.com/members/localhost" >@localhost< /a > ~]$ echo $abc
[chengmo<a href= "http://www.jobbole.com/members/localhost" >@localhost< /a > ~]$ echo ${abc= 'ok' }
ok
[chengmo<a href= "http://www.jobbole.com/members/localhost" >@localhost< /a > ~]$ echo $abc
ok
  
若是abc 没有声明“=" 还会给abc赋值。
[chengmo<a href= "http://www.jobbole.com/members/localhost" >@localhost< /a > ~]$ var1=11;var2=12;var3=
[chengmo<a href= "http://www.jobbole.com/members/localhost" >@localhost< /a > ~]$ echo ${! v @}
var1 var2 var3
[chengmo<a href= "http://www.jobbole.com/members/localhost" >@localhost< /a > ~]$ echo ${! v *}
var1 var2 var3
  
${!varprefix*}与${!varprefix@}类似,能够经过变量名前缀字符,搜索已经定义的变量,不管是否为空值

 

2、字符串操做(长度,读取,替换)编程

表达式 含义
${#string} $string的长度
${string:position} 在$string中, 从位置$position开始提取子串
${string:position:length} 在$string中, 从位置$position开始提取长度为$length的子串
${string#substring} 从变量$string的开头, 删除最短匹配$substring的子串
${string##substring} 从变量$string的开头, 删除最长匹配$substring的子串
${string%substring} 从变量$string的结尾, 删除最短匹配$substring的子串
${string%%substring} 从变量$string的结尾, 删除最长匹配$substring的子串
${string/substring/replacement} 使用$replacement, 来代替第一个匹配的$substring
${string//substring/replacement} 使用$replacement, 代替全部匹配的$substring
${string/#substring/replacement} 若是$string的前缀匹配$substring, 那么就用$replacement来代替匹配到的$substring
${string/%substring/replacement} 若是$string的后缀匹配$substring, 那么就用$replacement来代替匹配到的$substring

说明:”* $substring”能够是一个正则表达式.windows

1.长度app

1
2
3
[web97@salewell97 ~]$ test = 'I love china'
[web97@salewell97 ~]$ echo ${ #test}
12

${#变量名}获得字符串长度函数

2.截取字串性能

1
2
3
4
5
[chengmo<a href= "http://www.jobbole.com/members/localhost" >@localhost< /a > ~]$ test = 'I love china'
[chengmo<a href= "http://www.jobbole.com/members/localhost" >@localhost< /a > ~]$ echo ${ test :5}
e china
[chengmo<a href= "http://www.jobbole.com/members/localhost" >@localhost< /a > ~]$ echo ${ test :5:10}
e china

${变量名:起始:长度}获得子字符串spa

3.字符串删除code

1
2
3
4
5
6
7
8
9
10
[chengmo<a href= "http://www.jobbole.com/members/localhost" >@localhost< /a > ~]$ test = 'c:/windows/boot.ini'
[chengmo<a href= "http://www.jobbole.com/members/localhost" >@localhost< /a > ~]$ echo ${ test #/}
c: /windows/boot .ini
[chengmo<a href= "http://www.jobbole.com/members/localhost" >@localhost< /a > ~]$ echo ${ test #*/}
windows /boot .ini
[chengmo<a href= "http://www.jobbole.com/members/localhost" >@localhost< /a > ~]$ echo ${ test ##*/}
boot.ini
[chengmo<a href= "http://www.jobbole.com/members/localhost" >@localhost< /a > ~]$ echo ${ test %/*}
c: /windows
[chengmo<a href= "http://www.jobbole.com/members/localhost" >@localhost< /a > ~]$ echo ${ test %%/*}

${变量名#substring正则表达式}从字符串开头开始配备substring,删除匹配上的表达式。

${变量名%substring正则表达式}从字符串结尾开始配备substring,删除匹配上的表达式。

注意:${test##*/},${test%/*} 分别是获得文件名,或者目录地址最简单方法。

4.字符串替换

1
2
3
4
5
[chengmo<a href= "http://www.jobbole.com/members/localhost" >@localhost< /a > ~]$ test = 'c:/windows/boot.ini'
[chengmo<a href= "http://www.jobbole.com/members/localhost" >@localhost< /a > ~]$ echo ${ test /\ // \\}
c:\windows /boot .ini
[chengmo<a href= "http://www.jobbole.com/members/localhost" >@localhost< /a > ~]$ echo ${ test // \ // \\}
c:\windows\boot.ini

${变量/查找/替换值} 一个“/”表示替换第一个,”//”表示替换全部,当查找中出现了:”/”请加转义符”\/”表示。

 

3、性能比较

在shell中,经过awk,sed,expr 等均可以实现,字符串上述操做。下面咱们进行性能比较。

1
2
3
4
5
6
7
8
[chengmo<a href= "http://www.jobbole.com/members/localhost" >@localhost< /a > ~]$ test = 'c:/windows/boot.ini'
[chengmo<a href= "http://www.jobbole.com/members/localhost" >@localhost< /a > ~]$ time for i in $( seq 10000); do a=${ #test};done;
real 0m0.173s
user 0m0.139s
sys 0m0.004s
[chengmo<a href= "http://www.jobbole.com/members/localhost" >@localhost< /a > ~]$ time for i in $( seq 10000); do a=$( expr length $ test ); done ;
real 0m9.734s
user 0m1.628s

速度相差上百倍,调用外部命令处理,与内置操做符性能相差很是大。在shell编程中,尽可能用内置操做符或者函数完成。使用awk,sed相似会出现这样结果。

若是想深刻体验LINUX系统的新手,也能够先下载一个方德Linux软件中心试用一下。
免费下载地址:http://www.nfs-cloud.cn:81/appCenter/open/softcenter

相关文章
相关标签/搜索