zoukankan      html  css  js  c++  java
  • Shell脚本中替换字符串等操作

    在做shell批处理程序时候,常常会涉及到字符串相关操作。

    有非常多命令语句。如:awk,sed都能够做字符串各种操作。 事实上shell内置一系列操作符号。能够达到类似效果,大家知道,使用内部操作符会省略启动外部程序等时间,因此速度会非常的快。

     

    一、推断读取字符串值

    表达式 含义
    ${var} 变量var的值, 与$var同样
       
    ${var-DEFAULT} 假设var没有被声明, 那么就以$DEFAULT作为其值 *
    ${var:-DEFAULT} 假设var没有被声明, 或者其值为空, 那么就以$DEFAULT作为其值 *
       
    ${var=DEFAULT} 假设var没有被声明, 那么就以$DEFAULT作为其值 *
    ${var:=DEFAULT} 假设var没有被声明, 或者其值为空, 那么就以$DEFAULT作为其值 *
       
    ${var+OTHER} 假设var声明了, 那么其值就是$OTHER, 否则就为null字符串
    ${var:+OTHER} 假设var被设置了, 那么其值就是$OTHER, 否则就为null字符串
       
    ${var?ERR_MSG} 假设var没被声明, 那么就打印$ERR_MSG *
    ${var:?ERR_MSG} 假设var没被设置, 那么就打印$ERR_MSG *
       
    ${!varprefix*} 匹配之前全部以varprefix开头进行声明的变量
    ${!varprefix@} 匹配之前全部以varprefix开头进行声明的变量

    增加了“*”  不是意思是: 当然, 假设变量var已经被设置的话, 那么其值就是$var.

    [chengmo@localhost ~]$ echo ${abc-'ok'}
    ok
    [chengmo@localhost ~]$ echo $abc

    [chengmo@localhost ~]$ echo ${abc='ok'}
    ok
    [chengmo@localhost ~]$ echo $abc
    ok

     

    假设abc 没有声明“=" 还会给abc赋值。

    [chengmo@localhost ~]$ var1=11;var2=12;var3=
    [chengmo@localhost ~]$ echo ${!v@}            
    var1 var2 var3
    [chengmo@localhost ~]$ echo ${!v*}
    var1 var2 var3

     

    ${!varprefix*}与${!varprefix@}相似,能够通过变量名前缀字符,搜索已经定义的变量,不管是否为空值。

     

    二、字符串操作(长度,读取。替换)

    表达式 含义
    ${#string} $string的长度
       
    ${string:position} 在$string中, 从位置$position開始提取子串
    ${string:position:length} 在$string中, 从位置$position開始提取长度为$length的子串
       
    ${string#substring} 从变量$string的开头, 删除最短匹配$substring的子串
    ${string##substring} 从变量$string的开头, 删除最长匹配$substring的子串
    ${string%substring} 从变量$string的结尾, 删除最短匹配$substring的子串
    ${string%%substring} 从变量$string的结尾, 删除最长匹配$substring的子串
       
    ${string/substring/replacement} 使用$replacement, 来取代第一个匹配的$substring
    ${string//substring/replacement} 使用$replacement, 取代全部匹配的$substring
    ${string/#substring/replacement} 假设$string的前缀匹配$substring, 那么就用$replacement来取代匹配到的$substring
    ${string/%substring/replacement} 假设$string的后缀匹配$substring, 那么就用$replacement来取代匹配到的$substring
       

    说明:"* $substring”能够是一个正則表達式.

     

    1.长度

    [web97@salewell97 ~]$ test='I love china'
    [web97@salewell97 ~]$ echo ${#test}
    12

    ${#变量名}得到字符串长度

     

    2.截取字串

    [chengmo@localhost ~]$ test='I love china'
    [chengmo@localhost ~]$ echo ${test:5}     
    e china
    [chengmo@localhost ~]$ echo ${test:5:10} 
    e china

    ${变量名:起始:长度}得到子字符串

     

    3.字符串删除

    [chengmo@localhost ~]$ test='c:/windows/boot.ini'
    [chengmo@localhost ~]$ echo ${test#/}
    c:/windows/boot.ini
    [chengmo@localhost ~]$ echo ${test#*/}
    windows/boot.ini
    [chengmo@localhost ~]$ echo ${test##*/}
    boot.ini

    [chengmo@localhost ~]$ echo ${test%/*} 
    c:/windows
    [chengmo@localhost ~]$ echo ${test%%/*}

    ${变量名#substring正則表達式}从字符串开头開始配备substring,删除匹配上的表达式。

    ${变量名%substring正則表達式}从字符串结尾開始配备substring,删除匹配上的表达式。

    注意:${test##*/},${test%/*} 各自是得到文件名称,或者文件夹地址最简单方法。

    4.字符串替换

    [chengmo@localhost ~]$ test='c:/windows/boot.ini'
    [chengmo@localhost ~]$ echo ${test///\}
    c:windows/boot.ini
    [chengmo@localhost ~]$ echo ${test////\}
    c:windowsoot.ini

     

    ${变量/查找/替换值} 一个“/”表示替换第一个,”//”表示替换全部,当查找中出现了:”/”请加转义符”/”表示。

    三、性能比較

    在shell中。通过awk,sed,expr 等都能够实现,字符串上述操作。以下我们进行性能比較。

    [chengmo@localhost ~]$ test='c:/windows/boot.ini'                       
    [chengmo@localhost ~]$ time for i in $(seq 10000);do a=${#test};done;           

    real    0m0.173s
    user    0m0.139s
    sys     0m0.004s

    [chengmo@localhost ~]$ time for i in $(seq 10000);do a=$(expr length $test);done;      

    real    0m9.734s
    user    0m1.628s

     

    速度相差上百倍,调用外部命令处理,与内置操作符性能相差很大。在shell编程中。尽量用内置操作符或者函数完毕。

    使用awk,sed类似会出现这样结果。

  • 相关阅读:
    Treap 树堆 容易实现的平衡树
    (转)Maven实战(二)构建简单Maven项目
    (转)Maven实战(一)安装与配置
    根据请求头跳转判断Android&iOS
    (转)苹果消息推送服务器 php 证书生成
    (转)How to renew your Apple Push Notification Push SSL Certificate
    (转)How to build an Apple Push Notification provider server (tutorial)
    (转)pem, cer, p12 and the pains of iOS Push Notifications encryption
    (转)Apple Push Notification Services in iOS 6 Tutorial: Part 2/2
    (转)Apple Push Notification Services in iOS 6 Tutorial: Part 1/2
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/7041379.html
Copyright © 2011-2022 走看看