教程集 www.jiaochengji.com
教程集 >  脚本编程  >  shell  >  正文 shell字符串截取方法分享

shell字符串截取方法分享

发布时间:2014-12-08   编辑:jiaochengji.com
本文介绍下,在shell编程中,用于字符串截取的方法,包括用于字符串截取的特殊变量,有需要的朋友参考下。

本节内容:
shell字符串截取

方法1,使用特殊变量法:
 

${varible##*string }   从左向右截取最后一个string后的字符串
${varible#*string}      从左向右截取第一个string后的字符串
${varible%%string*}  从右向左截取最后一个string后的字符串
${varible%string*}      从右向左截取第一个string后的字符串

例子:
 

复制代码 代码示例:
TESTSTRING="teststring helloworld"
 echo ${TESTSTRING##*s}
        "tring helloworld"
 echo ${TESTSTRING#*s}
        "tstring helloworld"
 echo ${TESTSTRING%%s*}
        "te"
echo ${TESTSTRING%s*}
        "test"

方法2:
 

复制代码 代码示例:

${varible:n1:n2}   截取从n1到n2长度的字符串
 TESTSTRING="teststring helloworld"

echo  ${TESTSTRING:0:4}
        "test"

 echo  ${TESTSTRING:1:5}
        "ests"

echo ${TESTSTRING:0:-2}
        "teststring hellowor"

以上就是本节介绍的shell编程中,用于字符串截取的方法,希望对大家有所帮助。

您可能感兴趣的文章:
shell截取字符串的例子
shell字符串截取方法分享
有关shell中字符串截取的方法总结
shell字符串截取的几种方法
php中文字符截取函数(自用)
shell 字符串的处理(截取,连接,匹配,替换,翻转)
php如何截取字符串后四位
有关字符串截取的shell脚本(多个方法)
php中文截取字符串函数(很好用)
shell 字符串查找与替换的方法详解

[关闭]
~ ~