shell截取字符串的方法.docx
《shell截取字符串的方法.docx》由会员分享,可在线阅读,更多相关《shell截取字符串的方法.docx(9页珍藏版)》请在冰豆网上搜索。
![shell截取字符串的方法.docx](https://file1.bdocx.com/fileroot1/2022-11/23/04215432-dd75-4385-9cf3-91007caa1031/04215432-dd75-4385-9cf3-91007caa10311.gif)
shell截取字符串的方法
shell截取字符串的方法
shell字符串的截取的问题:
一、Linuxshell截取字符变量的前8位,有方法如下:
1.exprsubstr“$a”18
2.echo$a|awk‘{printsubstr(,1,8)}’
3.echo$a|cut-c1-8
4.echo$
5.expr$a:
‘\(.\\).*’
6.echo$a|ddbs=1count=82>/dev/null
二、按指定的字符串截取
1、第一种方法:
${varible##*string}从左向右截取最后一个string后的字符串
${varible#*string}从左向右截取第一个string后的字符串
${varible%%string*}从右向左截取最后一个string后的字符串
${varible%string*}从右向左截取第一个string后的字符串
“*”只是一个通配符可以不要
例子:
$MYVAR=foodforthought.jpg
$echo${MYVAR##*fo}
rthought.jpg
$echo${MYVAR#*fo}
odforthought.jpg
2、第二种方法:
${varible:
n1:
n2}:
截取变量varible从n1到n2之间的字符串。
可以根据特定字符偏移和长度,使用另一种形式的变量扩展,来选择特定子字符串。
试着在bash中输入以下行:
$EXCLAIM=cowabunga
$echo${EXCLAIM:
0:
3}
cow
$echo${EXCLAIM:
3:
7}
abunga
这种形式的字符串截断非常简便,只需用冒号分开来指定起始字符和子字符串长度。
三、按照指定要求分割:
比如获取后缀名
ls-al|cut-d“.”-f2
一、构造字符串
直接构造
STR_ZERO=hello
STR_FIRST="iamastring"
STR_SECOND='success'
重复多次
#repeatthefirstparm($1)by$2times
strRepeat()
{
localx=$2
if["$x"==""];then
x=0
fi
localSTR_TEMP=""
while[$x-ge1];
do
STR_TEMP=`printf"%s%s""$STR_TEMP""$1"`
x=`expr$x-1`
done
echo$STR_TEMP
}
举例:
STR_REPEAT=`strRepeat"$USER_NAME"3`
echo"repeat=$STR_REPEAT"
二、赋值与拷贝
直接赋值
与构造字符串一样
USER_NAME=terry
从变量赋值
ALIASE_NAME=$USER_NAME
三、联接
直接联接两个字符串
STR_TEMP=`printf"%s%s""$STR_ZERO""$USER_NAME"`
使用printf可以进行更复杂的联接
四、求长
求字符数(char)
COUNT_CHAR=`echo"$STR_FIRST"|wc-m`
echo$COUNT_CHAR
求字节数(byte)
COUNT_BYTE=`echo"$STR_FIRST"|wc-c`
echo$COUNT_BYTE
求字数(word)
COUNT_WORD=`echo"$STR_FIRST"|wc-w`
echo$COUNT_WORD
五、比较
相等比较
str1=str2
不等比较
str1!
=str2
举例:
if["$USER_NAME"="terry"];then
echo"Iamterry"
fi
小于比较
#return0ifthetwostringisequal,return1if$1<$2,else2strCompare(){localx=0if["$1"!
="$2"];thenx=2localTEMP=`printf"%s\n%s""$1""$2"`localTEMP2=`(echo"$1";echo"$2")|sort`if["$TEMP"="$TEMP2"];thenx=1fifiecho$x}
六、测试
判空
-zstr
判非空
-nstr
是否为数字
#return0ifthestringisnum,otherwise1
strIsNum()
{
localRET=1
if[-n"$1"];then
localSTR_TEMP=`echo"$1"|sed's/[0-9]//g'`
if[-z"$STR_TEMP"];then
RET=0
fi
fi
echo$RET
}
举例:
if[-n"$USER_NAME"];then
echo"mynameisNOTempty"
fi
echo`strIsNum"9980"`
七、分割
以符号+为准,将字符分割为左右两部分
使用sed
举例:
命令date--rfc-3339seconds的输出为
2007-04-1415:
09:
47+08:
00
取其+左边的部分
date--rfc-3339seconds|sed's/+[0-9][0-9]:
[0-9][0-9]//g'
输出为
2007-04-1415:
09:
47
取+右边的部分
date--rfc-3339seconds|sed's/.*+//g'
输出为
08:
00
以空格为分割符的字符串分割
使用awk
举例:
STR_FRUIT="Banana0.89100"
取第3字段
echo$STR_FRUIT|awk'{print$3;}'
八、子字符串
字符串1是否为字符串2的子字符串
#return0if$1issubstringof$2,otherwise1
strIsSubstring()
{
localx=1
case"$2"in
*$1*)x=0;;
esac
echo$x
}
在做shell批处理程序时候,经常会涉及到字符串相关操作。
有很多命令语句,如:
awk,sed都可以做字符串各种操作。
其实shell内置一系列操作符号,可以达到类似效果,大家知道,使用内部操作符会省略启动外部程序等时间,因此速度会非常的快。
一、判断读取字符串值
表达式
含义
${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.
[chengmo@localhost~]$echo${abc-'ok'}
ok
[chengmo@localhost~]$echo$abc
[chengmo@localhost~]$echo${abc='ok'}
ok
[chengmo@localhost~]$echo$abc
ok
如果abc没有声明“="还会给abc赋值。
[chengmo@localhost~]$var1=11;var2=12;var3=
[chengmo@localhost~]$echo${!
v@}
var1var2var3
[chengmo@localhost~]$echo${!
v*}
var1var2var3
${!
varprefix*}与${!
varprefix@}相似,可以通过变量名前缀字符,搜索已经定义的变量,无论是否为空值。
二、字符串操作(长度,读取,替换)
表达式
含义
${#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”可以是一个正则表达式.
1.长度
[web97@salewell97~]$test='Ilovechina'
[web97@salewell97~]$echo${#test}
12
${#变量名}得到字符串长度
2.截取字串
[chengmo@localhost~]$test='Ilovechina'
[chengmo@localhost~]$echo${test:
5}
echina
[chengmo@localhost~]$echo${test:
5:
10}
echina
${变量名:
起始:
长度}得到子字符串
3.字符串删除
[chengmo@localhost~]$test='c:
/windows/boot.ini'
[chengmo@localhost~]$echo${test#/}
c:
/windows/boot.ini
[chengmo@localhost~]$echo${test#*/}
windows/boot.ini
[chengmo@localhost~]$echo${test##*/}
boot.ini
[chengmo@localhost~]$echo${test%/*}
c:
/windows
[chengmo@localhost~]$echo${test%%/*}
${变量名#substring正则表达式}从字符串开头开始配备substring,删除匹配上的表达式。
${变量名%substring正则表达式}从字符串结尾开始配备substring,删除匹配上的表达式。
注意:
${test##*/},${test%/*}分别是得到文件名,或者目录地址最简单方法。
4.字符串替换
[chengmo@localhost~]$test='c:
/windows/boot.ini'
[chengmo@localhost~]$echo${test/\//\\}
c:
\windows/boot.ini
[chengmo@localhost~]$echo${test//\//\\}
c:
\windows\boot.ini
${变量/查找/替换值}一个“/”表示替换第一个,”//”表示替换所有,当查找中出现了:
”/”请加转义符”\/”表示。
三、性能比较
在shell中,通过awk,sed,expr等都可以实现,字符串上述操作。
下面我们进行性能比较。
[chengmo@localhost~]$test='c:
/windows/boot.ini'
[chengmo@localhost~]$timeforiin$(seq10000);doa=${#test};done;
real 0m0.173s
user 0m0.139s
sys 0m0.004s
[chengmo@localhost~]$timeforiin$(seq10000);doa=$(exprlength$test);done;
real 0m9.734s
user 0m1.628s
速度相差上百倍,调用外部命令处理,与内置操作符性能相差非常大。
在shell编程中,尽量用内置操作符或者函数完成。
使用awk,sed类似会出现这样结