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 ...
随机推荐
- CSS3中的px,em,rem,vh,vw辨析
1.px:像素,精确显示 2.em:继承父类字体的大小,相当于"倍",如:浏览器默认字体大小为16px=1em,始终按照div继承来的字体大小显示,进场用于移动端 em换算工具:h ...
- adb connect 出现timeout的处理方式
默认的timeout是5000ms毫秒,如果是断点调试的时候,经常会出现timeout, 所以修改这个值就不会出现timeout了, Eclipse下的设置位置: Window->Prefere ...
- SQL中NULL值
SQL的表达式,除了IS NULL和NOT NULL以外,只要出现NULL值结果都为FALSE 简单的例子: SELECT * FROM table WHERE name!='abc' 只要name值 ...
- Spring用代码来读取properties文件
我们都知道,Spring可以@Value的方式读取properties中的值,只需要在配置文件中配置org.springframework.beans.factory.config.PropertyP ...
- .net项目中上传的图片或者文件太大 无法上传
最近做项目的时候 用户提出要上传大图片 一张图片有可能十几兆 本来用的第三方的上传控件 有限制图片上传大小的设置 以前设置的是2M 按照用户的要求 以为直接将限制图片上传大小的设置改下就可 ...
- php_zip 类
<?php /** * Created by PhpStorm. * User: StarZhan * Date: 2016/5/24 * Time: 12:04 */class PHPZip{ ...
- IE11兼容性问题修改
最近测试给了我一大堆BUG,一瞅发现全是IE11的.吐槽一下这个浏览器真的比较特立独行.很多默认的样式跟别的浏览器不同,而且最明显的一点应该是padding左右内边距往往比别的浏览器大了一倍.但是当需 ...
- Android安全攻防战,反编译与混淆技术完全解析(上)
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/49738023 之前一直有犹豫过要不要写这篇文章,毕竟去反编译人家的程序并不是什么值 ...
- Best code水题之路
BestCoder 2nd Anniversary: 1001.Oracle There is once a king and queen, rulers of an unnamed city, wh ...
- Echarts 3.19 制作常用的图形 非静态
最近阿里内部使用的 图表也向外开放了 而百度就好像更有良心一点,Echarts 早就开放了 . 自己学Echarts的时候走了很多的弯路,毕竟谁让自己菜呢,多撞几次南墙才晓得疼 才知道学习方法,新手上 ...