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 ...
随机推荐
- JSTL-1
JSTL的配置和使用: * 配置:将jstl.jar和standard.jar拷贝到WEB-INF/lib下 * 使用:要采用一些指令:采用taglib指令 JSTL标准标签库(JSP Standar ...
- 小数据池,bytes
'''python2 python3 '''#python2#print() print 'abc'#range() xrange() 生成器# raw_input() #python3#print( ...
- 我的OI生涯 第六章
开学了,但是我们并没有像一个正常的高二学生一样坐在教室里接受调研考试的洗礼. 暑假作业这种东西早已被甩在一旁,可以想象回去补文化课时该有多么狼狈. 大王给我们制定了周密的计划,每周两次测试,加上蔡老师 ...
- bzoj 3595
Splay 每个节点维护一个区间. /************************************************************** Problem: 3595 User ...
- bzoj 1468
大概思路:树点分治,重心树中每个重心维护一个总的平衡树,树中保存属于该重心的点到该重心的距离,然后对于去掉该重心后形成的子树分别再保存一份. 用这种方式实现的话,还可以支持修改与多次查询,每次操作都是 ...
- April Fools Day Contest 2016 G. You're a Professional
G. You're a Professional 题目连接: http://www.codeforces.com/contest/656/problem/G Description A simple ...
- keras入门--Mnist手写体识别
介绍如何使用keras搭建一个多层感知机实现手写体识别及搭建一个神经网络最小的必备知识 import keras # 导入keras dir(keras) # 查看keras常用的模块 ['Input ...
- 在阅读sqlmap源码时学到的知识--检查运行环境
最近在读sqlmap的源码,懵懵懂懂中页大约学到了一些知识(说给自己听的话:由此可见,所谓的能够解决所有遇到问题的python水平,只能说明你遇见的都是简单的需求....),老规矩,在这里写一下,一则 ...
- Spring MapFactoryBean例子
MapFactoryBean类为开发者提供了一种在Spring的bean配置文件中创建一个具体的Map集合类(HashMap和TreeMap). 这里有一个MapFactoryBean.例如,在运行时 ...
- linux里install命令和cp命令的区别
转:http://blog.yikuyiku.com/?p=2659 基本上,在Makefile里会用到install,其他地方会用cp命令. 它们完成同样的任务——拷贝文件,它们之间的区别主要如下: ...