Copy codeThe Code is as follows:
Filename = '/home/admin/jobs/CnClickstat/DFSLoader/loader. cfg'
# Use shell strings
BuName1 =1 {filename # */jobs/} # Get 'cnclickstat/DFSLoader/loader. cfg 'by removing the'/home/admin/jobs/CnClickstat/DFSLoader/loader. cfg 'prefix'
BuName1 =1 {buName1 %/*} # Remove the 'cnclickstat/DFSLoader/loader. cfg 'suffix to get 'cnclickstat'
Echo $ buName1
# Use awk to obtain the required string content
BuName2 = 'echo $ filename | awk-F/'{printf ("% s", $5 )}'';
Echo $ buName2
# Use cut to obtain the required string content
BuName3 = 'echo $ filename | cut-d/-f 5 ';
Echo $ buName3
The above results can be obtained: CnClickstat
String operation example ends
String-related operations are often involved in shell batch processing. There are many command statements, such as awk and sed, which can perform various string operations. In fact, shell has a series of built-in operation symbols, which can achieve similar results. As you know, using internal operators will omit the time to start external programs, so the speed will be very fast.
I. Judge to read the string value
Expression |
Description |
$ {Var} |
The value of the variable var, which is the same as that of $ var. |
|
|
$ {Var-DEFAULT} |
If var is not declared, use $ DEFAULT as its value * |
$ {Var:-DEFAULT} |
If var is not declared or its value is null, $ DEFAULT is used as its value * |
|
|
$ {Var = DEFAULT} |
If var is not declared, use $ DEFAULT as its value * |
$ {Var: = DEFAULT} |
If var is not declared or its value is null, $ 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, Print $ ERR_MSG * |
$ {Var :? ERR_MSG} |
If var is not set, Print $ ERR_MSG * |
|
|
$ {! Varprefix *} |
Match all variables declared starting with varprefix |
$ {! Varprefix @} |
Match all variables declared starting with varprefix |
"*" Does not mean: of course, if the variable var has been set, its 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 assign a 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 *} and $ {! Varprefix @} is similar. You can use the variable name prefix to search for Defined variables, whether or not they are null.
2. String operations (length, read, replace)
Expression |
Description |
$ {# String} |
$ String Length |
|
|
$ {String: position} |
In $ string, the substring is extracted from position $ position. |
$ {String: position: length} |
In $ string, the Child string with the length of $ length is extracted from the position $ position. |
|
|
$ {String # substring} |
From the beginning of the variable $ string, delete the substring that matches the minimum $ substring. |
$ {String ## substring} |
Removes the substring that matches $ substring at the beginning of the variable $ string. |
$ {String % substring} |
Removes the substring that matches the minimum value of $ substring from the end of the variable $ string. |
$ {String % substring} |
Removes the substrings that match the longest $ substring from the end of the variable $ string. |
|
|
$ {String/substring/replacement} |
Use $ replacement to replace the first matched $ substring. |
$ {String // substring/replacement} |
Use $ replacement insteadAllMatched $ substring |
$ {String/# substring/replacement} |
If $ string'sPrefix$ Substring is matched, and $ replacement is used to replace the matched $ substring. |
$ {String/% substring/replacement} |
If $ string'sSuffix$ Substring is matched, and $ replacement is used to replace the matched $ substring. |
Note: "* $ substring" can be a regular expression.
1. Length
[Web97 @ salewell97 ~] $ Test = 'I love China'
[Web97 @ salewell97 ~] $ Echo $ {# test}
12
$ {# Variable name} returns the string length
2. truncate a 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 obtain the substring
3. Delete string
[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 % /*}
$ {Variable name # substring Regular Expression} starts with a string and starts with a substring to delete matching expressions.
$ {Variable name % substring Regular Expression} starts from the end of the string with substring to delete the matching expression.
Note: $ {test # */} and $ {test %/*} are the simplest methods to get the file name or directory address.
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
$ {Variable/search/replace value} A "/" indicates replacing the first, "//" indicates replacing all. When the search appears: "/" Please add an escape character.
Iii. Performance Comparison
In shell, awk, sed, and expr can all be used to perform the preceding operations on strings. Next we will compare the performance.
[Chengmo @ localhost ~] $ Test = 'C:/windows/boot. ini'
[Chengmo @ localhost ~] $ Time for I in $ (seq 10000); doa =$ {# test}; done;
Real 0m0. 173 s
User 0m0. 139 s
Sys 0m0. 004 s
[Chengmo @ localhost ~] $ Time for I in $ (seq 10000); do a =$ (expr length $ test); done;
Real 0m9. 734 s
User 0m1. 628 s
The speed is hundreds of times different. Calling external command processing is very different from the built-in operator performance. In shell programming, use built-in operators or functions as much as possible. When awk is used, sed will display such results.
Yorking Alan