bash shell中expr命令下几种的使用
expr在linux中是一个功能非常强大的命令。通过学习做一个小小的总结。
1、计算字符串的长度。我们可以用awk中的length(s)进行计算。我们也可以用echo中的echo ${#string}进行计算,当然也可以expr中的expr length $string 求出字符串的长度。
举例
[root@localhost shell]# string="hello,everyone my name is xiaoming"
[root@localhost shell]# echo ${#string}
34
[root@localhost shell]# expr length "$string"
34
2、expr中的expr index stringsubstring索引命令功能在字符串string substring索引命令功能在字符串stringsubstring索引命令功能在字符串string上找出substring中字符第一次出现的位置,若找不到则expr index返回0或1。
举例
[root@localhost shell]# string="hello,everyone my name is xiaoming"
[root@localhost shell]# expr index "$string" my
11
[root@localhost shell]# expr index "$string" nihao
1
3、expr中的expr match $string substring命令在string字符串中匹配substring字符串,然后返回匹配到的substring字符串的长度,若找不到则返回0。
举例
[root@localhost shell]# string="hello,everyone my name is xiaoming"
[root@localhost shell]# expr match "$string" my
0
[root@localhost shell]# expr match "$string" hell.*
34
[root@localhost shell]# expr match "$string" hell
4
[root@localhost shell]# expr match "$string" small
0
4、在shell中可以用{string:position}和{string:position:length}进行对string字符串中字符的抽取。第一种是从position位置开始抽取直到字符串结束,第二种是从position位置开始抽取长度为length的子串。而用expr中的expr substr $string $position $length同样能实现上述功能。
举例
root@localhost shell]# string="hello,everyone my name is xiaoming"
[root@localhost shell]# echo ${string:10}
yone my name is xiaoming
[root@localhost shell]# echo ${string:10:5}
yone
[root@localhost shell]# echo ${string:10:10}
yone my na
[root@localhost shell]# expr substr "$string" 10 5
ryone
注意:echo string:10:5和exprsubstr"{string:10:5}和 expr substr "string:10:5和exprsubstr"string" 10 5的区别在于string:10:5以0开始标号而exprsubstr"{string:10:5}以0开始标号而expr substr "string:10:5以0开始标号而exprsubstr"string" 10 5以1开始标号。
5、删除字符串和抽取字符串相似KaTeX parse error: Expected '}', got '#' at position 8: {string#̲substring}为删除st…{string##substring}为删除string开头处与substring匹配的最长字符子串。
举例
[root@localhost shell]# string="20091111 readnow please"
[root@localhost shell]# echo ${string#2*1}
111 readnow please
[root@localhost shell]# string="20091111 readnow please"
[root@localhost shell]# echo ${string##2*1}
readnow please
解析:第一个为删除2和1之间最短匹配,第二个为删除2和1之间的最长匹配。
6、替换子串string/substring/replacement表示仅替换一次substring相配字符,而{string/substring/replacement}表示仅替换一次substring相配字符,而string/substring/replacement表示仅替换一次substring相配字符,而{string//substring//replacement}表示为替换所有的substring相配的子串。
举例
[root@localhost shell]# string="you and you with me"
[root@localhost shell]# echo ${string/you/me}
me and you with me
[root@localhost shell]# string="you and you with me"
[root@localhost shell]# echo ${string//you/me}
me and me with me
bash shell中expr命令下几种的使用相关推荐
- python getopts_linux bash shell 中getopts 命令 和 python 中 getopt 函数的比较总结
在 python 中有个获取命令行参数的函数叫 getopt(args, shortopts, longopts=[]) 通常我们使用的时候是如下的形式: import sys import geto ...
- linux bash and,linux bash shell中for的用法and示例
关于linux bash shell中的for语句 在linux中shell是必不可少的一部分,但是在bash shell中有while,for,until等循环命令,今天就介绍一下关于for的一些用 ...
- repo命令添加bin_Linux shell中getopts命令学习--实现一个添加yum源的脚本
这是本人第一次写博客,之前从一些开源技术网站上看到不少大牛和前辈们的文章,从中学习受益.本着开源界的奉献和学习精神,觉得有必要将自己的学习成果拿出来与大家一起交流分享,既当作是一种自我学习的总结也可能 ...
- sh执行文件 参数传递_详解shell中脚本参数传递的两种方式
方式一:$0,$1,$2.. 采用$0,$1,$2..等方式获取脚本命令行传入的参数,值得注意的是,$0获取到的是脚本路径以及脚本名,后面按顺序获取参数,当参数超过10个时(包括10个),需要使用${ ...
- linux shell中的命令自动补全(compgen complete)与 命令行参数解析
linux shell中的命令自动补全(compgen complete)与 命令行参数解析 标签: shell脚本 2013-12-31 21:56 6661人阅读 评论(6) 收藏 举报 分类: ...
- shell中declare命令
shell中declare命令 declare命令有如下选项: -a 声明一个数组 -i 声明一个整型 -f 打印所有函数定义 -F 仅打印函数名字 -r 声明一个readonly变量,该变量的值无法 ...
- shell中source命令与sh命令的区别
一.source命令 1.1 source命令的使用方法 source filename.sh 文件没有可执行权限时,也可以使用source命令执行. source命令是在当前shell中执行的,并未 ...
- Bash Shell 注释多行的几种方法(转)
很实用的小技巧. 我们shell脚本写好了,但是想一行一行测试,怎么办. 笨方法:每行前面加一个 #,有时候我们原脚本里面本来就有注释,所以想再恢复的时候就麻烦了. Bash Shell 注释多行的几 ...
- Bash Shell中16进制数字和ASCII字符相互转换
Bash Shell中ASCII值和字符之间的转换 1.数值转换为ASCII字符 方法一: i=97echo $i | awk '{printf("%c", $1)}' 方法二: ...
最新文章
- python3 image与 图像io互转
- 简单比较python语言和c语言的异同-Python快速入门之与C语言异同
- java变量只声明未初始化是否可以直接使用?
- 兄弟割席:HTML5标准制定组织分裂
- python可以和java共存吗_python3 and python2 共存
- oracle里面的degree,收集统计信息degree不生效的问题
- mysqli_connect函数未开启_CRMEB打通版linux系统部署客服开启长链接不成功
- LaTeX 文字带边框
- Intel NUC的win10系统安装教程
- axure 侧滑抽屉式菜单_Axure教程之抽屉菜单
- 身份证真伪辨别python版
- 翻译go项目代码英文注释
- Vue:如何制作表格数据分页查询
- 七大IT业界顶尖认证证书
- Python大数据+人工智能 学科视频教程
- 如何统计代码总行数:指令
- IP媒体服务器:3G视频业务的基石
- 有哪些是python做的软件_用Python写过哪些「脑洞大开」的小工具?
- 使用switch语句根据消费金额计算折扣
- 【名词解释】跑步pb是什么意思?
热门文章
- Java异常之More than one table found in namespace (, ) t_user
- ESXI6.7 windows虚拟机 取消Vmware Tools分辨率自适应/固定分辨率
- 怎样将应用程序发布到Apple store上去 (三)
- h5手机端页面自适应屏幕大小 rem
- 为什么 Go 语言没有泛型
- 深度剖析 Go 的 nil
- centos7设置go代理
- c语言中结构体变量的作用,C语言 结构体 类型变量的 定义 和 引用
- 使用Beetle简单构建高性能Socket tcp应用
- 【那千古永恒不变的信念】