String-related operations are often involved in the shell batch process. There are a lot of command statements, such as: awk,sed can do various operations of string. In fact, the Shell has a series of operating symbols, you can achieve similar effects, you know, using the internal operator will omit the start of the external program and so on time, so the speed will be very fast.
First, judge read string value
An expression |
meaning |
${var} |
Variable var with the same value as $var |
|
|
${var-default} |
If Var is not declared, then use $default as its value * |
${var:-default} |
If Var is not declared, or if its value is null, then the value is $default |
|
|
${var=default} |
If Var is not declared, then use $default as its value * |
${var:=default} |
If Var is not declared, or if its value is null, then the value is $default |
|
|
${var+other} |
If Var is declared, then its value is $other, otherwise it is a null string |
${var:+other} |
If Var is set, the value is $other, otherwise it is a null string |
|
|
${var? ERR_MSG} |
If Var is not declared, then print $err_msg * |
${var:? ERR_MSG} |
If Var is not set, then print $err_msg * |
|
|
${!varprefix*} |
Match all previously declared variables preceded by Varprefix |
${!varprefix@} |
Match all previously declared variables preceded by Varprefix |
Adding "*" does not mean: Of course, if Var has been set, the value is $var.
[chengmo@localhost ~]$ echo ${abc-'ok'}
OK
[chengmo@localhost ~]$ echo $abc
[chengmo@localhost ~]$ echo ${abc='ok'}
OK
[chengmo@localhost ~]$ echo $abc
OK
If ABC does not declare "=" it will also assign value to ABC.
[chengmo@localhost ~]$ var1=11;var2=12;var3=
[chengmo@localhost ~]$ echo ${!v@}
var1 var2 var3
[chengmo@localhost ~]$ echo ${!v*}
var1 var2 var3
${! Varprefix *} is similar to ${! Varprefix @}. You can search the defined variable by prefixing the variable name with characters, whether it is null or not.
Second, string operation (length, read, replace)
An expression |
meaning |
${#string} |
Length of $string |
|
|
${string:position} |
In $string, the substring is extracted from the position $position |
${string:position:length} |
In $string, a substring of length $length is extracted from the position $position. |
|
|
${string#substring} |
Deletes the substring of the shortest matching $substring from the beginning of the variable $string |
${string# #substring} |
Deletes the substring of the longest matching $substring from the beginning of the variable $string |
${string%substring} |
Deletes the substring of the shortest matching $substring from the end of the variable $string |
${string%%substring} |
Deletes the substring of the longest matching $substring from the end of the variable $string |
|
|
${string/substring/replacement} |
Use $replacement to replace the first matching $substring |
${string//substring/replacement} |
Use $replacement to replace all matching $substring |
${string/#substring/replacement} |
If the $string prefix matches the $substring, the $replacement is used instead of the matching $substring |
${string/%substring/replacement} |
If the $string suffix matches the $substring, then the $replacement is used instead of the matching $substring |
Description: "* $substring" can be a regular expression.
1. length
[web97@salewell97 ~]$ test='I love china'
[web97@salewell97 ~]$ echo ${#test}
Twelve
${ා variable name} to get string length
2. Truncation string
[chengmo@localhost ~]$ test='I love china'
[chengmo@localhost ~]$ echo ${test:5}
E China
[chengmo@localhost ~]$ echo ${test:5:10}
E China
${variable name: Start: length} to get substring
3. String deletion
[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%%/*}
The ${variable name ා substring regular expression} is equipped with substring from the beginning of the string to remove the expression on the match.
${variable name% substring regular expression} is equipped with substring from the end of the string, removing the expression on the match.
Note: ${test {test% / *} and ${test% / *} are the easiest ways to get the file name or directory address, respectively.
4. String replacement
[chengmo@localhost ~]$ test='c:/windows/boot.ini'
[chengmo@localhost ~]$ echo ${test/\//\\}
c:\windows/boot.ini
[chengmo@localhost ~]$ echo ${test//\//\\}
c:\windows\boot.ini
One '/' of ${variable / find / replace value} means to replace the first one, and '/ /' means to replace all. When '/' appears in the search, please add the escape character '\ /' to indicate.
Third, performance comparison
In the shell, through awk,sed,expr and so on can be implemented, string above operations. Here's a performance comparison.
[Chengmo@localhost ~]$ test= ' C:/windows/boot.ini '
[Chengmo@localhost ~]$ time to I in $ (seq 10000);d o a=${#test};d one;
Real 0m0.173s
User 0m0.139s
SYS 0m0.004s
[Chengmo@localhost ~]$ time to I in $ (seq 10000);d o a=$ (expr length $test);d one;
Real 0m9.734s
User 0m1.628s
The speed difference is hundredfold, calls the external command processing, and the built-in operator performance difference is very big. In shell programming, try to do it with built-in operators or functions. Similar results can occur with awk,sed.