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 ...
随机推荐
- spring-cloud学习BUG小结
1.com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: c ...
- (VIJOS) VOJ 1067 Warcraft III 守望者的烦恼 矩阵快速幂
https://vijos.org/p/1067 就..挺普通的一道题..自己学一下怎么推式子就可以...细节不多但是我还是日常爆细节..比如说循环写成从负数开始... 只求ac不求美观的丑陋 ...
- [CodeForces-797F]Mice and Holes
题目大意: 在一条直线上,有n个老鼠,m个洞. 每个老鼠i都有一个初始位置x[i]. 每个洞i都有一个固定位置p[i]和容量限制c[i]. 求所有老鼠都进洞的最小距离总和. 思路: 动态规划. 用f[ ...
- 快速幂 cojs 1130. 取余运算
cojs 1130. 取余运算 ★ 输入文件:dmod.in 输出文件:dmod.out 简单对比时间限制:10 s 内存限制:128 MB [题目描述] 输入b,p,k的值,求b^p ...
- 洛谷P4009 汽车加油行驶问题
题目描述 给定一个 N \times NN×N 的方形网格,设其左上角为起点◎,坐标(1,1)(1,1),XX 轴向右为正, YY 轴向下为正,每个方格边长为 11 ,如图所示. 一辆汽车从起点◎出发 ...
- MS SQL语句优化
MS SQL Server查询优化方法查询速度慢的原因很多,常见如下几种 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应. 3.没有创建计 ...
- Codeforces Round #305 (Div. 1) B. Mike and Feet 单调栈
B. Mike and Feet Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/547/pro ...
- mybatis源码分析(2)-----SqlSession创建
1. 在创建好sqlSessionFactory之后,接着就要配置sqlSession的创建. <bean id="simpleTempalte" class="o ...
- Android 动画——属性动画Property Animation
Android在3.0之前只提供了两种动画:View Animation .Drawable Animation .也就是我们在<Android 动画——Frame Animation与Twee ...
- asp.net 链接数据库ADO.NET
web.config <configuration> <connectionStrings> <add name="constr" connectio ...