当前位置:首页 > CN2资讯 > 正文内容

linux egrep命令详解 expr linux

2天前CN2资讯

 名称: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
(8)、模式匹配
说明:expr也有模式匹配功能。可以使用expr通过指定冒号选项计算字符串中字符数。.*意即任何字符重复0次或多次。
> VALUE=account.doc
> expr $VALUE : ‘.*’
8
在expr中可以使用字符串匹配操作,这里使用模式抽取.doc文件附属名。
$expr $VALUE : ‘\(.*\).doc’
accounts

 

 

 

 

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 $string substring索引命令功能在字符串$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
  • [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}和 expr substr "$string" 10 5的区别在于${string:10:5}以0开始标号而expr substr "$string" 10 5以1开始标号。

     

    5、删除字符串和抽取字符串相似${string#substring}为删除string开头处与substring匹配的最短字符子串,而${string##substring}为删除string开头处与substring匹配的最长字符子串。

    举例

  • [root@localhost shell]# string="20091111 readnow please" 
  • [root@localhost shell]# echo ${string#2*1} 
  • 111
  • [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相配的子串。

    举例

  • [root@localhost shell]# string="you and you with me" 
  • [root@localhost shell]# echo ${string/you/me} 
  • me and
  • [root@localhost shell]# string="you and you with me" 
  • [root@localhost shell]# echo ${string//you/me} 
  • me and
  •  

      你可能想看:

      扫描二维码推送至手机访问。

      版权声明:本文由皇冠云发布,如需转载请注明出处。

      本文链接:https://www.idchg.com/info/25780.html

      分享给朋友:

      “linux egrep命令详解 expr linux” 的相关文章

      香港虚拟主机CN2怎么用的:全面解析与实用指南

      香港虚拟主机CN2怎么用的香港虚拟主机CN2的选型指南与核心优势在全球化的今天,选择一个合适的虚拟主机服务对于网站的运营至关重要。而香港虚拟主机CN2凭借其独特的地理位置和高速稳定的网络连接,成为了众多站长和企业的首选。但面对市场上琳琅满目的选项,如何选择一款适合自己的香港虚拟主机CN2呢?本文将带...

      ExtraVM测评:美国优质VPS服务全面解析

      在美国,ExtraVM是一家备受关注的主机商,提供的VPS方案在业内小有名气。这家企业不仅因其强大的硬件配置而受到用户热爱,还因为具备高带宽和强大防御能力而赢得了良好的口碑。对于许多站长来说,这里就像是一块“宝地”,能够满足他们各种需求。 当我第一次了解ExtraVM的时候,我被其在洛杉矶的数据中心...

      搬瓦工DC9:高性价比VPS选择,稳定快速的服务器解决方案

      在这个快速发展的互联网时代,越来越多的人开始寻求高效、稳定的服务器解决方案。搬瓦工DC9正是为满足这种需求而推出的一款限量版VPS套餐。它不仅方便快捷,而且在性能和性价比上都表现出色,让用户在搭建网站、进行游戏或其他项目时更加省心。 搬瓦工DC9的全名是“The DC9 Plan”,每年仅需38美元...

      域名购买推荐:如何选择最适合你的域名注册商

      域名购买推荐概述 在互联网时代,域名显得尤为重要。对于个人用户、企业甚至是初创团队来说,域名不仅是网站的门面,更是品牌形象和业务宣传的基石。记得我第一次建立网站时,选择一个合适的域名让我意识到它的价值。一个容易记住、与品牌相关的域名可以有效吸引流量,提升访问者的信任感。 在选购域名时,有几点基本原则...

      阿里云香港轻量服务器:高性价比云计算解决方案

      阿里云香港轻量服务器是我在寻找云计算解决方案时发现的一个非常实用的选择。它不仅具备高性价比,还有灵活的配置和便捷的管理体验,适合各种用户需求。让我带你深入了解一下这个产品的特点和优势。 首先,香港轻量服务器的价格设置比较合理。我看到它提供多个配置供用户选择,无论是新手还是有经验的开发者都能在这里找到...

      2023年美国服务器市场分析与未来展望

      在美国,服务器市场一直以来都具有举足轻重的地位。到了2023年,这个市场依旧保持着强劲的增长势头。根据IDC的分析报告,2023年第一季度,美国的服务器市场规模达到了2212亿美元,相比去年增长了2%。这不仅显示了市场的健康发展,也奠定了美国在全球服务器市场的领导地位,全球市场份额约为30%。 随着...