Bash String Manipulation Examples – Length, Substring, Find
In bash shell,when you use a dollar sign followed by a variable name,shell expands the variable with its value. This feature of shell is called parameter expansion. But parameter expansion has numerous other forms which allow you to expand a parameter and modify the value or substitute other values in the expansion process. In this article,let us review how to use the parameter expansion concept for string manipulation operations. This article is part of the on-going bash tutorial series. Refer to our earlier article on?. 1. Identify String Length inside Bash Shell Script${#string} The above format is used to get the length of the given bash variable. $ len.! /bin/var=<span style="color: #800000;">"<span style="color: #800000;">Welcome to the geekstuff<span style="color: #800000;">"
<span style="color: #0000ff;">echo<span style="color: #000000;"> ${#var} $ ./len.<span style="color: #0000ff;">sh To understand more about bash variables,read?. 2. Extract a Substring from a Variable inside Bash Shell ScriptBash 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. $ substr.! /bin/var=<span style="color: #800000;">"<span style="color: #800000;">Welcome to the geekstuff<span style="color: #800000;">"
<span style="color: #0000ff;">echo ${var:<span style="color: #800080;">15<span style="color: #000000;">} $ ./substr.<span style="color: #0000ff;">sh<span style="color: #000000;"> Also,refer to our earlier article to understand more about?. 3. Shortest Substring MatchFollowing 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. $ shortest.! /bin/filename=<span style="color: #800000;">"<span style="color: #800000;">bash.string.txt<span style="color: #800000;">"
<span style="color: #0000ff;">echo ${filename#<span style="color: #000000;">.} $ ./shortest.<span style="color: #0000ff;">sh<span style="color: #000000;"> 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 MatchFollowing 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. $ longest.! /bin/filename=<span style="color: #800000;">"<span style="color: #800000;">bash.string.txt<span style="color: #800000;">"
<span style="color: #0000ff;">echo <span style="color: #800000;">"<span style="color: #800000;">After deletion of longest match from front:<span style="color: #800000;">" ${filename##<span style="color: #000000;">.} $ ./longest.<span style="color: #0000ff;">sh<span style="color: #000000;"> 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 ScriptReplace 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. $ firstmatch.! /bin/filename=<span style="color: #800000;">"<span style="color: #800000;">bash.string.txt<span style="color: #800000;">"
<span style="color: #0000ff;">echo <span style="color: #800000;">"<span style="color: #800000;">After Replacement:<span style="color: #800000;">" ${filename/str*./<span style="color: #000000;">operations.} $ ./firstmatch.<span style="color: #0000ff;">sh<span style="color: #000000;"> Replace all the matches${string//pattern/replacement} It replaces all the matches of pattern with replacement. $ allmatch.! /bin/filename=<span style="color: #800000;">"<span style="color: #800000;">Path of the bash is /bin/bash<span style="color: #800000;">"
<span style="color: #0000ff;">echo <span style="color: #800000;">"<span style="color: #800000;">After Replacement:<span style="color: #800000;">" ${filename<span style="color: #008000;">//<span style="color: #008000;">bash/sh} Taking about find and replace,refer to our earlier articles –??and?. 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. $ posmatch.! /bin/filename=<span style="color: #800000;">"<span style="color: #800000;">/root/admin/monitoring/process.sh<span style="color: #800000;">"
<span style="color: #0000ff;">echo <span style="color: #800000;">"<span style="color: #800000;">Replaced at the beginning:<span style="color: #800000;">" ${filename/#/root//<span style="color: #000000;">tmp} $ ./posmatch.<span style="color: #0000ff;">sh<span style="color: #000000;"> reference from:http://www.thegeekstuff.com/2010/07/bash-string-manipulation/ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |