注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

osdba的博客

敢于担当,拥抱变化,锐意进取,永不止步

 
 
 

日志

 
 

Bash String Manipulation Examples – Length, Substring, Find and Replace  

2012-07-26 16:36:09|  分类: shell |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

1. Identify String Length inside Bash Shell Script

${#string}

The above format is used to get the length of the given bash variable.

$ cat len.sh #! /bin/bash  var="Welcome to the geekstuff"  echo ${#var}  $ ./len.sh 24 

To understand more about bash variables, read 6 Practical Bash Global and Local Variable Examples.

2. Extract a Substring from a Variable inside Bash Shell Script

Bash provides a way to extract a substring from a string. The following example expains how to parse n characters starting from a particular position.

${string:position}

Extract substring from $string at $position

${string:position:length}

Extract $length of characters substring from $string starting from $position. In the below example, first echo statement returns the substring starting from 15th position. Second echo statement returns the 4 characters starting from 15th position. Length must be the number greater than or equal to zero.

$ cat substr.sh #! /bin/bash  var="Welcome to the geekstuff"  echo ${var:15} echo ${var:15:4}  $ ./substr.sh geekstuff geek 

Also, refer to our earlier article to understand more about $*, $@, $#, $$, $!, $?, $-, $_ bash special parameters.


3. Shortest Substring Match

Following syntax deletes the shortest match of $substring from front of $string

${string#substring}

Following syntax deletes the shortest match of $substring from back of $string

${string%substring}

Following sample shell script explains the above two shortest substring match concepts.

$ cat shortest.sh #! /bin/bash  filename="bash.string.txt"  echo ${filename#*.} echo ${filename%.*}  $ ./shortest.sh After deletion of shortest match from front: string.txt After deletion of shortest match from back: bash.string 

In the first echo statement substring ‘*.’ matches the characters and a dot, and # strips from the front of the string, so it strips the substring “bash.” from the variable called filename. In second echo statement substring ‘.*’ matches the substring starts with dot, and % strips from back of the string, so it deletes the substring ‘.txt’

4. Longest Substring Match

Following syntax deletes the longest match of $substring from front of $string

${string##substring}

Following syntax deletes the longest match of $substring from back of $string

${string%%substring}

Following sample shell script explains the above two longest substring match concepts.

$ cat longest.sh #! /bin/bash  filename="bash.string.txt"  echo "After deletion of longest match from front:" ${filename##*.} echo "After deletion of longest match from back:" ${filename%%.*}  $ ./longest.sh After deletion of longest match from front: txt After deletion of longest match from back: bash 

In the above example, ##*. strips longest match for ‘*.’ which matches “bash.string.” so after striping this, it prints the remaining txt. And %%.* strips the longest match for .* from back which matches “.string.txt”, after striping  it returns “bash”.

5. Find and Replace String Values inside Bash Shell Script

Replace only first match

${string/pattern/replacement}

It matches the pattern in the variable $string, and replace only the first match of the pattern with the replacement.

$ cat firstmatch.sh #! /bin/bash  filename="bash.string.txt"  echo "After Replacement:" ${filename/str*./operations.}  $ ./firstmatch.sh After Replacement: bash.operations.txt 

Replace all the matches

${string//pattern/replacement}

It replaces all the matches of pattern with replacement.

$ cat allmatch.sh #! /bin/bash  filename="Path of the bash is /bin/bash"  echo "After Replacement:" ${filename//bash/sh}  $ ./allmatch.sh After Replacement: Path of the sh is /bin/sh 

Taking about find and replace, refer to our earlier articles – sed substitute examples and Vim find and replace.

Replace beginning and end

${string/#pattern/replacement

Following syntax replaces with the replacement string, only when the pattern matches beginning of the $string.

${string/%pattern/replacement

Following syntax replaces with the replacement string, only when the pattern matches at the end of the given $string.

$ cat posmatch.sh #! /bin/bash  filename="/root/admin/monitoring/process.sh"  echo "Replaced at the beginning:" ${filename/#\/root/\/tmp} echo "Replaced at the end": ${filename/%.*/.ksh}  $ ./posmatch.sh Replaced at the beginning: /tmp/admin/monitoring/process.sh Replaced at the end: /root/admin/monitoring/process.ksh 


  评论这张
 
阅读(598)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017