Linux Shell 截取字符串
Linux Shell 截取字符串
shell中截取字符串的方法很多
${var#*/}
${var##*/}
${var%/*}
${var%%/*}
${var:start:len}
${var:start}
${var:0-start:len}
${var:0-start}
下面用几个例子展示一下:
1) 获得字符串的长度
语法:
${#var}
示例代码:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]" length=${#str}
echo "length : [${length}]"
执行结果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
length : []
2) 使用 # 和 ## 获取尾部子字符串
2.1) # 最小限度从前面截取word
语法:
${parameter#word}
示例代码:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]" #分割符为'/'
substr=${str#*/}
echo "substr : [${substr}]"
执行结果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [/www.fengbohello.xin3e.com/blog/shell-truncating-string]
2.2) ## 最大限度从前面截取word
语法:
${parameter##word}
示例代码:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]" #分割符为'/'
substr=${str##*/}
echo "substr : [${substr}]"
执行结果:
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 : [${str}]" substr=${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 : [${str}]" substr=${str%%/*}
echo "substr : [${substr}]"
执行结果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [http:]
4)使用 ${var:} 模式获取子字符串
4.1) 指定从左边第几个字符开始以及子串中字符的个数
语法:
${var:start:len}
示例代码:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]" #其中的 表示左边第一个字符开始, 表示子字符的总个数。
substr=${str::}
echo "substr : [${substr}]"
执行结果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [http://]
4.2) 从左边第几个字符开始一直到结束
语法:
${var:}
示例代码:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]" #其中的 表示左边第8个字符开始
substr=${str:}
echo "substr : [${substr}]"
执行结果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [www.fengbohello.xin3e.com/blog/shell-truncating-string]
4.3) 从右边第几个字符开始以及字符的个数
语法:
${var:-start:len}
示例代码:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]" #其中的 - 表示右边算起第23个字符开始, 表示字符的个数
substr=${str:-:}
echo "substr : [${substr}]"
执行结果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [shell]
4.4) 从右边第几个字符开始一直到结束
语法:
${var:-start}
示例代码:
str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]" #其中的 - 表示右边算起第6个字符开始
substr=${str:-}
echo "substr : [${substr}]"
执行结果:
string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [string]
Linux Shell 截取字符串的更多相关文章
- shell截取字符串的方法
参考文献: linux中shell截取字符串方法总结 [Linux]如何在Shell脚本中计算字符串长度? 截取字符串的方法一共有八种,主要为以下方法 shell中截取字符串的方法有很多中, ${ex ...
- shell截取字符串方法
shell中截取字符串的方法有很多中, ${expression}一共有9种使用方法.${parameter:-word}${parameter:=word}${parameter:?word}${p ...
- shell截取字符串的8种方法
参考文献: linux中shell截取字符串方法总结 [Linux]如何在Shell脚本中计算字符串长度? 截取字符串的方法一共有八种,主要为以下方法 shell中截取字符串的方法有很多中, ${ex ...
- Linux shell去除字符串中所有空格
Linux shell去除字符串中所有空格 echo $VAR | sed 's/ //g'
- linux中shell截取字符串方法总结
截取字符串的方法一共有八种,主要为以下方法 shell中截取字符串的方法有很多中, ${expression}一共有9种使用方法. ${parameter:-word} ${parameter:=wo ...
- inux中shell截取字符串方法总结
shell中截取字符串的方法有很多中, ${expression}一共有9种使用方法. ${parameter:-word} ${parameter:=word} ${parameter:?word} ...
- shell 截取字符串(转)
linux中对字符串的处理: 1.字符串分割例如 AAAAA-BBBBBB 按-分割去前后两部分 cut : [rich@localhost ~]$ str=AAAAA-BBBBBB[rich@l ...
- linux 中截取字符串
shell中截取字符串的方法有很多中,${expression}一共有9种使用方法.${parameter:-word}${parameter:=word}${parameter:?word}${pa ...
- Linux shell 获得字符串所在行数及位置
shell 获得字符串所在行数及位置 01 获取字符串所在的行数 方式一:用grep -n [root@root]# cat test apple bit create delect exe flow ...
随机推荐
- CSS的5种常用的垂直居中的方法
1.绝对定位上下百分之五十然后上外边距做外边距都是他的宽高的一半 #child{ width: 200px; height: 150px; position: absolute; left: 50%; ...
- 《jQuery知识点总结》(一)
write less do more写更少的代码实现更多的功能DOM:document object model (文件对象模型)选择器(选择元素的对象或者节点)id 选择器 $("#id& ...
- selenium web driver 实现截图功能
在验证某些关键步骤时,需要截个图来记录一下当时的情况 Webdriver截图时,需要引入 import java.io.File; import java.io.IOException; import ...
- struts2笔记(2)
<context-param> <param-name>pattern</param-name> <param-value>yyyy-MM-dd hh: ...
- Servlet调用过程
(1)在浏览器输入地址,浏览器先去查找hosts文件,将主机名翻译为ip地址,如果找不到就再去查询dns服务器将主机名翻译成ip地址. (2)浏览器根据ip地址和端口号访问服务器,组织http请求信息 ...
- java servlet
回顾 1三要素是什么? 入口(login.html) 处理(LoginServlet.java) 出口 (success.jsp) 2如何访问servlet http://IP:port/p ...
- python中date、datetime、string的相互转换
import datetime import time string转datetime str = '2012-11-19' date_time = datetime.datetime.strptim ...
- js与jq对数组的操作
一.数组处理 1.数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长 ...
- AndroidStudio使用过程中遇到的bug
Ref:http://www.cnblogs.com/jingmo0319/p/5781878.html 1. Error:Execution failed for task ':app:transf ...
- adb 的使用
打出log:adb logcat -s fliterName ps:(fliterName就是你的tag)