名称:expr 
  
  ### 字串长度 
  
  shell>> expr length "this is a test" 
  14 
  
  ### 数字商数 
  
  shell>> expr 14 % 9 
  5 
  
  ### 从位置处抓取字串 
  
  shell>> expr substr "this is a test" 3 5 
  is is 
  
  ### 数字串 only the first character 
  
  shell>> expr index "testforthegame" e 
  2 
  
  ### 字串真实重现 
  
  shell>> expr quote thisisatestformela 
  thisisatestformela

~~~~~~~~~~~~~~~~~

expr命令是一个手工命令行计数器,用于在UNIX/LINUX下求表达式变量的值,一般用于整数值,也可用于字符串。
–格式为:
expr Expression(命令读入Expression 参数,计算它的值,然后将结果写入到标准输出)
–参数应用规则:
用空格隔开每个项;
用 \ (反斜杠) 放在 shell 特定的字符前面;
对包含空格和其他特殊字符的字符串要用引号括起来

–expr用法实例讲解:
(1)、计算字串长度
 > expr length “this is a test”
 14
(2)、抓取字串
 > expr substr “this is a test” 3 5
 is is
(3)、抓取第一个字符数字串出现的位置
 > expr index “sarasara”  a
 2
(4)、字串真实重现
 > expr quote sara
 sara
(5)、整数运算
 > expr 14 % 9
 5
 > expr 10 + 10
 20
 > expr 1000 + 900
 1900
 > expr 30 / 3 / 2
 5
 > expr 30 \* 3 (使用乘号时,必须用反斜线屏蔽其特定含义。因为shell可能会误解显示星号的意义)
 90
 > expr 30 * 3
 expr: Syntax error
(6)、增量计数
说明:expr在循环中用于增量计算。先将变量初始化为0,然后循环值加1,反引号的用法为命令替代。
> LOOP=0
> LOOP=`expr $LOOP + 1`
(7)、数值测试
说明:用expr测试一个数。如果试图计算非整数,则会返回错误。
> rr=3.4
> expr $rr + 1
expr: non-numeric argument
> rr=5
> expr $rr + 1
6

在expr中可以使用字符串匹配操作,这里使用模式抽取.doc文件附属名。
$expr $VALUE : ‘\(.*\).doc’
accounts

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

linux中expr用法的更多相关文章

  1. Linux中find用法

    Linux中find用法 linux常用命令 find -name april* 在当前目录下查找以april开始的文件 find -name april* fprint file 在当前目录下查找以 ...

  2. linux中nl用法

    linux 中nl 命令使用 nl :添加行号打印 -b:   指定行号指定的方式,主要有两种:    -b a : 表示不论是否为空行,都同样列出行号    -b t : 如果有空行,则不列出那一行 ...

  3. linux中read用法

    read在while中的经常用法: root@ubuntu:/var/lib/logrotate :: # cat /etc/cron.daily/logrotate #!/bin/sh # Clea ...

  4. linux中sed用法

    sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法sed命令行格式为:         sed ...

  5. Linux中wget用法

    Wget简介:Linux系统中wget是一个下载文件的工具,它用在命令行下.对于Linux用户是必不可少的工具,我们经常要下载一些软件或从远程服务器恢复备份到本地服务器.wget支持HTTP,HTTP ...

  6. linux中convert用法

    转: 强大的convert命令 convert命令可以用来转换图像的格式,支持JPG, BMP, PCX, GIF, PNG, TIFF, XPM和XWD等类型,下面举几个例子:   convert  ...

  7. linux中xargs用法

    参数代换: xargs xargs 是在做什么的呢?就以字面上的意义来看, x 是加减乘除的乘号,args 则是 arguments (参数) 的意思,所以说,这个玩意儿就是在产生某个命令的参数的意思 ...

  8. cut,sort,awk,sed,tr,find,wc,uniq在Linux中的用法

    cut语法cut [-bn] [file]cut [-c] [file]cut [-df] [file] -b :以字节为单位进行分割.这些字节位置将忽略多字节字符边界,除非也指定了 -n 标志.-c ...

  9. linux中fuser用法详解

    fuser功能 fuser 可以显示出当前哪个程序在使用磁盘上的某个文件.挂载点.甚至网络端口,并给出程序进程的详细信息.  fuser显示使用指定文件或者文件系统的进程ID.默认情况下每个文件名后面 ...

随机推荐

  1. [SDOI2016][bzoj4514] 数字配对 [费用流]

    题面 传送门 思路 一个数字能且只能匹配一次 这引导我们思考:一次代表什么?代表用到一定上限(b数组)就不能再用,同时每用一次会产生价值(c数组) 上限?价值?网络流! 把一次匹配设为一点流量,那产生 ...

  2. elasticsearch备份与恢复4_使用ES-Hadoop将ES中的索引数据写入HDFS中

    背景知识见链接:elasticsearch备份与恢复3_使用ES-Hadoop将HDFS数据写入Elasticsearch中 项目参考<Elasticsearch集成Hadoop最佳实践> ...

  3. Codeforces Round #241 (Div. 2) B dp

    B. Art Union time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  4. C 语言实现 php base64_encode

    这是在网上找到的一段代码,因为需求不同,稍微做了下修改,有需要的朋友可以直接复制使用. unsigned char *base64_encode(const unsigned char *str, s ...

  5. iOS 代理为啥要用weak修饰?

    在开发中我们经常使用代理,或自己写个代理,而代理属性都用weak(assign)修饰,看过有些开发者用strong(retain),但并没发现有何不妥,也不清楚weak(assign)与strong( ...

  6. 如何在win2003下安装sql2008[多次安装sql2008失败者必看]

    原文发布时间为:2010-11-02 -- 来源于本人的百度文章 [由搬家工具导入] 如何在win2003下安装sql2008[多次安装sql2008失败者必看] 1. 安装win2003,升级全部补 ...

  7. lightgbm 学习资料汇总

    操作实例:https://blog.csdn.net/luoyexuge/article/details/72956491 中文文档:https://lightgbm.apachecn.org/cn/ ...

  8. 享元模式FlyweightPattern(转)

    解释一下概念:也就是说在一个系统中如果有多个相同的对象,那么只共享一份就可以了,不必每个都去实例化一个对象.比如说一个文本系统,每个字母定一个对象,那么大小写字母一共就是52个,那么就要定义52个对象 ...

  9. 基于c语言中调试工具的用法汇总(不包含gdb)【转】

    转自:http://www.jb51.net/article/36829.htm 是不是只有编译的时候才知道程序写了错误?有没有在未编译的时候就让机器帮你检查错误的工具呢? 答案是:有!! splin ...

  10. 解决Manjaro linux的中文输入

    系统安装完成后, 首先:安装中文输入法:pacman -S fcitx fcitx-libpinyin kcm-fcitx 接着:修改.xprofile 添加内容如下: export GTK2_RC_ ...