bash里关于string相关的处理

| 2 Comments | No TrackBacks
  • 取字符串的长度:${#VAR}
  • # a="HelloWorld"
    # echo ${#a}
    10
    
  • 字符串截断:${VAR:POSITION}${VAR:POSITION:LENGTH}
  • # a="HelloWorld"
    # echo ${a:5}
    World
    # echo ${a:4:3}
    oWo
    
  • 字符串匹配取最短:${VAR#SUBSTRING}${VAR%SUBSTRING}
  • # a="HelloWorld"
    # echo ${a#*o}
    World
    # echo ${a%o*}
    HelloW
    

    注:#是从前向后,并且*号是紧随着的,而%则是从后向前匹配。*号是放在最后的。

  • 字符串匹配取最长:${VAR#SUBSTRING}${VAR%SUBSTRING}
  • # a="HelloWorld"
    # echo ${a#*o}
    rld
    # echo ${a%o*}
    Hell
    
  • 字符串替换:${VAR//PATTERN/REPLACEMENT}
  • # a="HelloWorld"
    # echo ${a//World/Earth}
    HelloEarth
    
  • 确定匹配位置:expr STRING : REGEX
  • # a="HelloWorld"
    # expr $a : ".*o"
    7
    

    注:这里的REGEX从名字上就说明了是一个正则表达式。

Update:详细请参考

No TrackBacks

TrackBack URL: http://mt.khsing.net/cgi-bin/mt-tb.cgi/140

2 Comments

http://www.thegeekstuff.com/2010/07/bash-string-manipulation/ 翻译、转载请注明

Leave a comment

About this Entry

This page contains a single entry by Guixing published on July 23, 2010 4:05 PM.

Data Center基础设施革新 was the previous entry in this blog.

Statistics From Woodpecker.org.cn is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.