linux shell string manipulation (length, search, replace)
When doing shell batch programs, string-related operations are often involved. There are many command statements, such as: awk, sed can do various operations on strings. In fact, the shell has a series of built-in operation symbols, which can achieve similar effects. As you know, using internal operators will omit the time to start external programs and so on, so the speed will be very fast.
First, determine the read string value
Expression meaning
$ {var} The value of the variable var, the same as $ var
$ {var-DEFAULT} If var is not declared, then $ DEFAULT is used as its value *
$ {var: -DEFAULT} If var is not declared or its value is empty, then $ DEFAULT is used as its value *
$ {var = DEFAULT} If var is not declared, then $ DEFAULT is used as its value *
$ {var: = DEFAULT} If var is not declared or its value is empty, then $ DEFAULT is used as its value *
$ {var + OTHER} If var is declared, its value is $ OTHER, otherwise it is a null string
$ {var: + OTHER} If var is set, its 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 *} matches all variables declared with varprefix before
$ {! [email protected]} matches all previously declared variables that start with varprefix
Adding "*" does not mean: Of course, if the variable var is already set, its value is $ var.
[[email protected] ~] $ echo $ {abc-‘ok ’}
ok
[[email protected] ~] $ echo $ abc
[[email protected] ~] $ echo $ {abc = ‘ok’}
ok
[[email protected] ~] $ echo $ abc
ok
If abc does not declare "=", abc will also be assigned a value.
[[email protected] ~] $ var1 = 11; var2 = 12; var3 =
[[email protected] ~] $ echo $ {! [email protected]}
var1 var2 var3
[[email protected] ~] $ echo $ {! v *}
var1 var2 var3
$ {! varprefix *} is similar to $ {! [email protected]}. You can search for defined variables by prefixing the variable name with or without a null value.
String operations (length, read, replace)
Expression meaning
$ {# string} $ string length
$ {string: position} in $ string, extract substrings starting from position $ position
$ {string: position: length} In $ string, extract the substring of length $ length starting from position $ position
$ {string # substring} deletes the shortest substring matching $ substring from the beginning of the variable $ string
$ {string ## substring} deletes the longest substring matching $ substring from the beginning of the variable $ string
$ {string% substring} removes the shortest substring matching $ substring from the end of the variable $ string
$ {string %% substring} deletes the longest substring matching $ substring from the end of the variable $ string
$ {string / substring / replacement} uses $ replacement instead of the first matching $ substring
$ {string // substring / replacement} uses $ replacement instead of all matching $ substrings
$ {string / # substring / replacement} If the prefix of $ string matches $ substring, then $ replacement is used instead of the matched $ substring
$ {string /% substring / replacement} If the suffix of $ string matches $ substring, then $ replacement is used instead of the matched $ substring
Explanation: "* $ substring" can be a regular expression.
Length
[[email protected] ~] $ test = ‘I love china’
[[email protected] ~] $ echo $ {# test}
12
$ {# Variable name} to get the string length
2. Intercept strings
[[email protected] ~] $ test = ‘I love china’
[[email protected] ~] $ echo $ {test: 5}
e china
[[email protected] ~] $ echo $ {test: 5: 10}
e china
$ (Variable name: start: length) get substring
3. String deletion
[[email protected] ~] $ test = ‘c: /windows/boot.ini’
[[email protected] ~] $ echo $ {test # /}
c: /windows/boot.ini
[[email protected] ~] $ echo $ {test # * /}
windows / boot.ini
[[email protected] ~] $ echo $ {test ## * /}
boot.ini
[[email protected] ~] $ echo $ {test% / *}
c: / windows
[[email protected] ~] $ echo $ {test %% / *}
$ {Variable name # substring regular expression} Equipped with substring from the beginning of the string, delete the expression on the match.
$ {Variable name% substring regular expression} is equipped with a substring from the end of the string, removing the expression on the match.
Note: $ {test ## * /}, $ {test% / *} are the easiest ways to get the file name or directory address, respectively.
4. String replacement
[[email protected] ~] $ test = ‘c: /windows/boot.ini’
[[email protected] ~] $ echo $ {test / \ // \\}
c: \ windows / boot.ini
[[email protected] ~] $ echo $ {test // \ // \\}
c: \ windows \ boot.ini
$ {Variable / Find / Replace Value} A "/" means replace the first one, "//" means replace all, when the search appears: "/" Please add escape character "\ /" means
Performance comparison
In the shell, it can be achieved through awk, sed, expr, etc. The above operations are performed on strings. Let's compare the performance.
[[email protected] ~] $ test = ‘c: /windows/boot.ini’
[[email protected] ~] $ time for i in $ (seq 10000); do a = $ {# test}; done;
real 0m0.173s
user 0m0.139s
sys 0m0.004s
[[email protected] ~] $ time for i in $ (seq 10000); do a = $ (expr length $ test); done;
real 0m9.734s
user 0m1.628s
The speed difference is hundreds of times, and the external command processing is called, which is very different from the built-in operator performance. In shell programming, try to use built-in operators or functions. Using awk, sed will produce similar results.
Linux shell String Operations (length, find, replace) detailed