Linux shell String Operations (length, find, replace) detailed

Source: Internet
Author: User

Linux shell String Operations (length, find, replace) detailed

String-related operations are often involved in the shell batch process. There are a lot of command statements, such as: awk,sed can do string various operations. In fact, the shell built a series of operating symbols, you can achieve similar effects, you know, using internal operators will omit the start of external programs and other time, so the speed will be very fast.

First, Judge read the 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 its value is empty, then $default is used as its value *
${var=default} If Var is not declared, then use $default 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, then its value is $other, otherwise it is a null string
${var:+other} If Var is set, then its value is $other, otherwise it will be a null string
${var? ERR_MSG} If Var is not declared, print $err_msg *
${var:? ERR_MSG} If Var is not set, then print $err_msg *
${!varprefix*} Matches all previously declared variables beginning with Varprefix
${[email protected]} Matches all previously declared variables beginning with Varprefix

Adding "*" does not mean: Of course, if the variable var is already set, then 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 "=" It will also assign a value to ABC.

[Email protected] ~]$ var1=11;var2=12;var3= [[email protected] ~]$ echo ${[email protected]} var1 var2 va R3 [[email protected] ~]$ echo ${!v*} var1 var2 var3

${!varprefix*} similar to ${[email protected]}, a variable name prefix character can be used to search for a variable that has already been defined, regardless of whether it is a null value.

Second, string operation (length, read, replace)

An expression meaning
${#string} Length of $string
${string:position} In $string, start extracting substrings from position $position
${string:position:length} In $string, a substring of length $length is extracted starting from position $position
${string#substring} Delete the substring of the shortest match $substring from the beginning of the variable $string
${string# #substring} Delete the substring of the longest matching $substring from the beginning of the variable $string
${string%substring} Delete the substring of the shortest match $substring from the end of the variable $string
${string%%substring} Delete 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 instead of all matching $substring
${string/#substring/replacement} If the $string prefix matches the $substring, then 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 .

. length

[Email protected] ~]$ test= ' I love China ' [[email protected] ~]$ echo ${#test} 12

${#变量名} to get the string length

2. Intercept strings

[Email protected] ~]$ test= ' I love China ' [[E-mail protected] ~]$ echo ${test:5} e China [[email protected] ~]$ E Cho ${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 Prote CTED] ~]$ 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} starts with substring from the beginning of the string and deletes the expression on the match.

${variable name%substring regular expression} starts with substring from the end of the string and deletes the expression on the match.

Note: ${test##*/},${test%/*} is the simplest way to get the file name, or directory address, respectively.

4. String substitution

[Email protected] ~]$ test= ' C:/windows/boot.ini ' [[email protected] ~]$ echo ${test/\//\\} c:\windows/boot.ini [[email P Rotected] ~]$ Echo ${test//\//\\} C:\windows\boot.ini

${variable/find/Replace value} A "/" means replacing the first, "//" means replacing all, when the lookup appears: "/" Please add escape character "/" to indicate.

Third, performance comparison

In the shell, through awk,sed,expr and so on can be implemented, the string above operation. Below we perform a performance comparison.

[[email protected] ~]$ test= ' C:/windows/boot.ini ' [[email protected] ~]$ time for I in $ (seq 10           ;d o a=${#test};d one;

Real 0m0.173s user 0m0.139s sys 0m0.004s

[[Email protected] ~]$ time for I in $ (seq 10000);d o a=$ (length of expr $test);d one;

Real 0m9.734s User 0m1.628s

The speed difference is hundreds of times, call external command processing, and built-in operator performance is very different. In shell programming, try to complete with built-in operators or functions. Similar results can occur with awk,sed.

Linux shell String Operations (length, find, replace) detailed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.