linux下面字符串截取,linux 字符串截取

lishell中截取字符串的方法很多

[

0ac8778b4872

li

](javascript:void(0); "复制代码")

 
 

math?formula=%7Bvar%23*%2F%7D{var##/}

math?formula=%7Bvar%25%2F*%7D{var%%/}

math?formula=%7Bvar%3Astart%3Alen%7D{var:start}

math?formula=%7Bvar%3A0-start%3Alen%7D{var:0-start}

[

0ac8778b4872

复制代码

](javascript:void(0); "复制代码")

下面用几个例子展示一下:

1) 获得字符串的长度

语法:

${#var}

示例代码:

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"

echo "string : [

math?formula=%7Bstr%7D%5D%22%20length%3D{#str} echo "length : [${length}]"

执行结果:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]

length : [61]

2) 使用 # 和 ## 获取尾部子字符串

2.1) # 最小限度从前面截取word

语法:

${parameter#word}  

示例代码:

[

0ac8778b4872

复制代码

](javascript:void(0); "复制代码")

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"

echo "string : [

math?formula=%7Bstr%7D%5D%22%20%23%E5%88%86%E5%89%B2%E7%AC%A6%E4%B8%BA'%2F'%20substr%3D{str#*/} echo "substr : [${substr}]"

[

0ac8778b4872

复制代码

](javascript:void(0); "复制代码")

执行结果:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]

substr : [/www.fengbohello.xin3e.com/blog/shell-truncating-string]

2.2) ## 最大限度从前面截取word

语法:

${parameter##word}

示例代码:

[

0ac8778b4872

复制代码

](javascript:void(0); "复制代码")

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"

echo "string : [

math?formula=%7Bstr%7D%5D%22%20%23%E5%88%86%E5%89%B2%E7%AC%A6%E4%B8%BA'%2F'%20substr%3D{str##*/} echo "substr : [${substr}]"

[

0ac8778b4872

复制代码

](javascript:void(0); "复制代码")

执行结果:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]

substr : [shell-truncating-string]

3) 使用 % 和 %% 获取头部子字符串

3.1) % 最小限度从后面截取word

语法:

${parameter%word} 

示例代码:

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"

echo "string : [

math?formula=%7Bstr%7D%5D%22%20substr%3D{str%/*}

echo "substr : [${substr}]"

执行结果:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]

substr : [http://www.fengbohello.xin3e.com/blog]

3.2) %% 最大限度从后面截取word

语法:

${parameter%%word}

示例代码:

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"

echo "string : [

math?formula=%7Bstr%7D%5D%22%20substr%3D{str%%/*}

echo "substr : [${substr}]"

执行结果:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]

substr : [http:]

4)使用 ${var:} 模式获取子字符串

4.1) 指定从左边第几个字符开始以及子串中字符的个数

语法:

${var:start:len}

示例代码:

[

0ac8778b4872

复制代码

](javascript:void(0); "复制代码")

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"

echo "string : [

math?formula=%7Bstr%7D%5D%22%20%23%E5%85%B6%E4%B8%AD%E7%9A%84%200%20%E8%A1%A8%E7%A4%BA%E5%B7%A6%E8%BE%B9%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%AD%97%E7%AC%A6%E5%BC%80%E5%A7%8B%EF%BC%8C7%20%E8%A1%A8%E7%A4%BA%E5%AD%90%E5%AD%97%E7%AC%A6%E7%9A%84%E6%80%BB%E4%B8%AA%E6%95%B0%E3%80%82%20substr%3D{str:0:7} echo "substr : [${substr}]"

[

0ac8778b4872

复制代码

](javascript:void(0); "复制代码")

执行结果:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]

substr : [http://]

4.2) 从左边第几个字符开始一直到结束

语法:

${var:7}

示例代码:

[

0ac8778b4872

复制代码

](javascript:void(0); "复制代码")

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"

echo "string : [

math?formula=%7Bstr%7D%5D%22%20%23%E5%85%B6%E4%B8%AD%E7%9A%84%207%20%E8%A1%A8%E7%A4%BA%E5%B7%A6%E8%BE%B9%E7%AC%AC8%E4%B8%AA%E5%AD%97%E7%AC%A6%E5%BC%80%E5%A7%8B%20substr%3D{str:7} echo "substr : [${substr}]"

[

0ac8778b4872

复制代码

](javascript:void(0); "复制代码")

执行结果:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]

substr : [www.fengbohello.xin3e.com/blog/shell-truncating-string]

4.3) 从右边第几个字符开始以及字符的个数

语法:

${var:0-start:len}

示例代码:

[

0ac8778b4872

复制代码

](javascript:void(0); "复制代码")

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"

echo "string : [

math?formula=%7Bstr%7D%5D%22%20%23%E5%85%B6%E4%B8%AD%E7%9A%84%200-23%20%E8%A1%A8%E7%A4%BA%E5%8F%B3%E8%BE%B9%E7%AE%97%E8%B5%B7%E7%AC%AC23%E4%B8%AA%E5%AD%97%E7%AC%A6%E5%BC%80%E5%A7%8B%EF%BC%8C5%20%E8%A1%A8%E7%A4%BA%E5%AD%97%E7%AC%A6%E7%9A%84%E4%B8%AA%E6%95%B0%20substr%3D{str:0-23:5} echo "substr : [${substr}]"

[

0ac8778b4872

复制代码

](javascript:void(0); "复制代码")

执行结果:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]

substr : [shell]

4.4) 从右边第几个字符开始一直到结束

语法:

${var:0-start}

示例代码:

[

0ac8778b4872

复制代码

](javascript:void(0); "复制代码")

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"

echo "string : [

math?formula=%7Bstr%7D%5D%22%20%23%E5%85%B6%E4%B8%AD%E7%9A%84%200-6%20%E8%A1%A8%E7%A4%BA%E5%8F%B3%E8%BE%B9%E7%AE%97%E8%B5%B7%E7%AC%AC6%E4%B8%AA%E5%AD%97%E7%AC%A6%E5%BC%80%E5%A7%8B%20substr%3D{str:0-6} echo "substr : [${substr}]"

[

0ac8778b4872

复制代码

](javascript:void(0); "复制代码")

执行结果:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]

substr : [string]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值