shell中的字符串操作
SHELL字符串操作
bash Shell提供了多种字符串处理的命令:
- awk命令
- expr命令
字符串长度
- ${#..}
- expr length
- awk的length(s)
实例:
string=”hello world”
${#string}
expr length “$string”
注意:双引號是必须的。由于字符串有空格
匹配子串
格式:expr match $string $substring
作用:在string的开头匹配substring,返回匹配到的长度,在string开头匹配不到substring则返回0,substring能够是正則表達式
string=”welcome to our world”
| 命令 | 返回值 |
|---|---|
| expr match “$string” w.* | 20 |
| expr match “$string” ou.* | 0 |
公共字符的索引
格式:expr index $string $sunstring
作用:在字符串string上匹配substring中字符第一次出现的字符
string=”welcome to our world”
| 命令 | 返回值 |
|---|---|
| expr index “$string” our | 5 |
| expr index “$string” d | 20 |
| expr index “$string” s | 0 |
执行发现。expr index的功能是寻找两个串之间的第一个公共字符
截取子串
- 从左截取
- ${string:position}
- ${string:position:length}
- 从右截取
- ${string: -position}(冒号后面有一个空格)
- ${string:(position)}
- ${string: -position:length}
- ${string:(position):length}
expr substr
格式:expr substr $string $position $length
与${}的差别:${}的position从0開始给string标号;expr sutstr的position从1開始给string标号
string=”welcome to our world”
| 命令 | 返回值 |
|---|---|
| echo ${string:1:8} | elcome t |
| expr substr “$string” 2 8 | elcome t |
正則表達式截取子串
使用正則表達式仅仅能抽取string开头处或结尾处的子串。
- expr match $string ‘\($substring\)’
- expr $string : ‘\($substring\)’
| 命令 | 返回值 |
|---|---|
| expr match “$another” “[0-9]*” | 8 |
| expr match “$another” “\([0-9]*\)” | 20091114 |
| expr “$another” : “\([0-9]*\)” | 20091114 |
注意:冒号两側有空格
删除子串
- ${string#substring}
删除string开头处与substring匹配的最短子串 - ${string##substring}
删除string开头处与substring匹配的最长子串 - ${string%substring}
删除string结尾处与substring匹配的最短子串 - ${string%%substring}
- 删除string结尾处与substring匹配的最长子串
substring并不是正則表達式
20091114 Reading Hadoop
| 命令 | 结果 |
|---|---|
| echo “${another#2*1}” | 114 Reading Hadoop |
| echo “${another##2*1}” | 4 Reading Hadoop |
| echo “${another%a*p}” | 20091114 Reading H |
| echo “${another%%a*p}” | 20091114 Re |
替换子串
- ${string/substring/replacement}
仅替换第一次与substring相匹配的子串 - ${string//substring/replacement}
替换全部与substring相匹配的子串 - ${string/#substring/replacement}
替换string开头处与substring相匹配的子串 - ${string/%substring/replacement}
替换string结尾处与substring相匹配的子串
string=”20001020year20050509month”
| 命令 | 结果 |
|---|---|
| echo ${string/200/201} | 20101020year20050509month |
| echo ${string/200/201} | 20101020year20150509month |
| echo ${string/r*h/} | 20001020yea |
| echo ${string/#2000/2010} | 20101020year20050509month |
| echo ${string/%month/MONTH} | 20001020year20050509MONTH |
shell中的字符串操作的更多相关文章
- shell中的字符串操作和数学运算
字符串操作 变量赋值: 说明:变量值可以用单引号.双引号.或者不加任何引号来赋值给变量 变量名="变量值" 变量名='变量值' 变量名=变量值 例如:str="hel ...
- shell中的字符串操作——字符串的切割
default.yaml {default_baseurl: 'http://10.113.10.68:8082'} test.sh a=`cat default.yaml` t=":&qu ...
- linux shell学习(字符串操作)--01
http://blog.csdn.net/shuanghujushi/article/details/51298672 在bash shell的使用过程中,经常会遇到一些字符串string的操作,下面 ...
- (转)Shell中获取字符串长度的七种方法
Shell中获取字符串长度的七种方法 原文:http://blog.csdn.net/jerry_1126/article/details/51835119 求字符串操作在shell脚本中很常用,下面 ...
- shell中截取字符串的方法总结
shell中截取字符串的方法有很多种, ${expression}一共有9种使用方法. ${parameter:-word} ${parameter:=word} ${parameter:?word} ...
- SQL点滴33—SQL中的字符串操作
原文:SQL点滴33-SQL中的字符串操作 计算字符串长度len()用来计算字符串的长度 select sname ,len(sname) from student 字符串转换为大.小写lower() ...
- Python中的字符串操作总结(Python3.6.1版本)
Python中的字符串操作(Python3.6.1版本) (1)切片操作: str1="hello world!" str1[1:3] <=> 'el'(左闭右开:即是 ...
- Oracle中有关字符串操作的语法
Oracle中有关字符串操作的语法 Oracle提供了丰富的字符串函数 lpad()函数 lpad()函数用于左补全字符串.在某些情况下,预期的字符串为固定长度,而且格式统一,此时可以考虑使用lpad ...
- 一句python,一句R︱python中的字符串操作、中文乱码、NaN情况
一句python,一句R︱python中的字符串操作.中文乱码.NaN情况 先学了R,最近刚刚上手Python,所以想着将python和R结合起来互相对比来更好理解python.最好就是一句pytho ...
随机推荐
- codevs1081 线段树练习 2<区间修改>
1081 线段树练习 2 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作 1:给区间[a,b]的所有 ...
- HDU3853 LOOPS 期望DP 简单
http://acm.hdu.edu.cn/showproblem.php?pid=3853 有一点坑的地方是如果一个地方停在原地的概率为1,那么该地的期望为0,就相当于这个地方也是一个出口... ...
- 多个Fragment在屏幕翻转会重影问题的解决
fragment使用add和hide而不用replace的方法添加到activity中,如果屏幕翻转可能会又add新的fragment进去,所以会重影. 如果有一个sparsearray保存fragm ...
- 74.Interesting Sequence(有趣的数列)(拓扑排序)
Interesting Sequence(有趣的数列)[Special judge] 题目概述:是否存在一个长度为n的整数数列,其任意连续p项之和为正数而任意连续q项之和为负数? 方法:连续项a[i] ...
- 2015 UESTC 搜索专题J题 全都是秋实大哥 kmp
全都是秋实大哥 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Desc ...
- MSChart使用小结
在用到图表展示某项.多项信息的统计情况,很正常联想到MSChart控件. 以VS2008开发为例,在工具箱也中右击,选择”choose items“,打开对话框,选择COM组件T ...
- ExtJS ComboBox同时加载远程和本地数据
ExtJS ComboBox同时加载远程和本地数据 原文:http://gblog.hbcf.net/index.php/archives/233 ComboBox比较特殊需求,将远程数据和本地数据同 ...
- ios开发中object-c中UTF-8 和 GBK 的 NSString 相互转化的方法
应用都要遇到一个很头疼的问题:文字编码,汉字的 GBK 和 国际通用的 UTF-8 的互相转化稍一不慎, 就会满屏乱码.下面介绍 UTF-8 和 GBK 的 NSString 相互转化的方法 NS ...
- Nagios 监控mysqlserver具体实现过程
,之后在页面就能够看到监控效果了 參考文章:http://os.51cto.com/art/201409/452605.htm
- Supported_Hardware#4G_LTE_cards_and_modems
https://wiki.mikrotik.com/wiki/Supported_Hardware#4G_LTE_cards_and_modems