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
 
 

expr命令总结的更多相关文章

  1. expr 命令

    求表达式变量的值.expr 命令读入 Expression 参数,计算它的值,然后将结果写入到标准输出. 用法:expr Expression Expression 参数规则: 用空格隔开每个项. 用 ...

  2. 【转】linux expr命令参数及用法详解

    在抓包过程中,查看某个设定时间内,数据上下行多少,用命令expr 计算! --------------------------------------------------------------- ...

  3. expr命令

    expr命令的兩大作用:1)四则运算:2)字符串的操作: 1.四则运算 [tough@localhost ~]$ + + [tough@localhost ~]$ + [tough@localhost ...

  4. linux expr命令参数及用法详解

    expr用法 expr命令一般用于整数值,但也可用于字符串.一般格式为: #expr argument operator argument expr也是一个手工命令行计数器. #$expr 10 + ...

  5. linux下数学运算器:expr命令(shell中完成数学运算)

    expr用法  expr命令一般用于整数值,但也可用于字符串.一般格式为:  expr argument operator argument  expr也是一个手工命令行计数器.  $expr 10 ...

  6. SHELL脚本--expr命令全解

    bash&shell系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html expr命令可以实现数值运算.数值或字符串比较.字符串匹配.字 ...

  7. Linux之expr命令详解

    expr命令: expr命令是一个手工命令行计数器,用于在UNIX/LINUX下求表达式变量的值,一般用于整数值,也可用于字符串. –格式为: expr Expression(命令读入Expressi ...

  8. (转)linux expr命令参数及用法详解

    linux expr命令参数及用法详解 原文:http://blog.csdn.net/tianmohust/article/details/7628694 expr用法 expr命令一般用于整数值, ...

  9. linux之expr命令

    expr命令可以实现数值运算.数值或字符串比较.字符串匹配.字符串提取.字符串长度计算等功能.它还具有几个特殊功能,判断变量或参数是否为整数.是否为空.是否为0等. 先看expr命令的info文档in ...

  10. expr命令使用

    转载:http://www.cnblogs.com/f-ck-need-u/p/7231832.html expr命令可以实现数值运算.数值或字符串比较.字符串匹配.字符串提取.字符串长度计算等功能. ...

随机推荐

  1. telegraph.conf配置

    原文:http://www.cnblogs.com/smail-bao/p/7002361.html 1.操作系统基础监控指标配置标准 基础监控使用通用的全局配置文件telegraf.conf,以下只 ...

  2. SSH三大框架的工作原理

    Hibernate工作原理 原理:1.通过Configuration().configure();读取并解析hibernate.cfg.xml配置文件2.由hibernate.cfg.xml中的< ...

  3. 第09章:MongoDB-CRUD操作--文档--修改--update

    ①语法 db.collection.update( <query>, <update>, { upsert: <boolean>, multi: <boole ...

  4. Tensorflow RNN_LSTM实例

    RNN的一种类型模型被称为长短期记忆网络(LSTM).我觉得这是一个有趣的名字.它听起来也意味着:短期模式长期不会被遗忘. LSTM的精确实现细节不在本文的范围之内.相信我,如果只学习LSTM模型会分 ...

  5. AngularJS监听数组变化

    我们在使用angualr的监听时候,业务的需要我们会去监听一个数组的某一个值得变化,再写逻辑代码.然而我们在使用$scope.$watch("",function(){ })时候会 ...

  6. Struts2之ModelDriven和Preparable拦截器

    首先struts.xml文件配置如下 默认拦截器设置为paramsPrepareParamsStack <package name="default" namespace=& ...

  7. spring配置Bean

    Bean   就是一个类 一下代码都是基于spring之IOC和DI实现案例基础上进行解析 Bean的实例化方式: 1.无参构造 <bean id="UserService" ...

  8. 用jquery将输入的文字的双向绑定

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  9. Flume source 支持的type类型

    Flume是一个分布式的高可用的消费组件.通过修改配置文件,可以启动不同的agent处理不同来源的数据. agent包含source,channel,sink三个组件.今天我们学习下source的ty ...

  10. Android-Kotlin简单计算器功能

    上一篇博客 Android-Kotlin-配置/入门 配置好了 AndroidStudio Kotlin 的环境: 选择包名,然后右键: 选择Class类型,会有class: 创建CounterCla ...