随笔 - 366  文章 - 0  评论 - 101  阅读 - 30万

bash shell中expr命令下几种的使用(string)

http://www.linuxidc.com/Linux/2012-04/58095.htm

expr在linux中是一个功能非常强大的命令。通过学习做一个小小的总结。
1、计算字符串的长度。我们可以用awk中的length(s)进行计算。我们也可以用echo中的echo ${#string}进行计算,当然也可以expr中的expr length $string 求出字符串的长度。

举例

  1. [root@localhost shell]# string="hello,everyone my name is xiaoming"  
  2. [root@localhost shell]# echo ${#string}  
  3. 34  
  4. [root@localhost shell]# expr length "$string"  
  5. 34  

 

 

2、expr中的expr index $string substring索引命令功能在字符串$string上找出substring中字符第一次出现的位置,若找不到则expr index返回0或1。
举例
  1. [root@localhost shell]# string="hello,everyone my name is xiaoming"   
  2. [root@localhost shell]# expr index "$string" my   
  3. 11  
  4. [root@localhost shell]# expr index "$string" nihao   
  5. 1  
3、expr中的expr match $string substring命令在string字符串中匹配substring字符串,然后返回匹配到的substring字符串的长度,若找不到则返回0。
举例
  1. [root@localhost shell]# string="hello,everyone my name is xiaoming"   
  2. [root@localhost shell]# expr match "$string" my   
  3. 0  
  4. [root@localhost shell]# expr match "$string" hell.*   
  5. 34  
  6. [root@localhost shell]# expr match "$string" hell   
  7. 4  
  8. [root@localhost shell]# expr match "$string" small   
  9. 0  
4、在shell中可以用{string:position}和{string:position:length}进行对string字符串中字符的抽取。第一种是从position位置开始抽取直到字符串结束,第二种是从position位置开始抽取长度为length的子串。而用expr中的expr substr $string $position $length同样能实现上述功能。
举例
  1. root@localhost shell]# string="hello,everyone my name is xiaoming"   
  2. [root@localhost shell]# echo ${string:10}   
  3. yone my name is xiaoming  
  4. [root@localhost shell]# echo ${string:10:5}   
  5. yone  
  6. [root@localhost shell]# echo ${string:10:10}   
  7. yone my na  
  8. [root@localhost shell]# expr substr "$string" 10 5   
  9. ryone  

注意:echo ${string:10:5}和 expr substr "$string" 10 5的区别在于${string:10:5}以0开始标号而expr substr "$string" 10 5以1开始标号。

5、删除字符串和抽取字符串相似${string#substring}为删除string开头处与substring匹配的最短字符子串,而${string##substring}为删除string开头处与substring匹配的最长字符子串
举例
  1. [root@localhost shell]# string="20091111 readnow please"   
  2. [root@localhost shell]# echo ${string#2*1}   
  3. 111 readnow please  
  4. [root@localhost shell]# string="20091111 readnow please"   
  5. [root@localhost shell]# echo ${string##2*1}   
  6. readnow please  
解析:第一个为删除2和1之间最短匹配,第二个为删除2和1之间的最长匹配。
6、替换子串${string/substring/replacement}表示仅替换一次substring相配字符,而${string//substring//replacement}表示为替换所有的substring相配的子串
举例
  1. [root@localhost shell]# string="you and you with me"   
  2. [root@localhost shell]# echo ${string/you/me}   
  3. me and you with me  
  4. [root@localhost shell]# string="you and you with me"   
  5. [root@localhost shell]# echo ${string//you/me}   
  6. me and me with me  
posted on   寒星12345678999  阅读(483)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示