Shell string Processing

Source: Internet
Author: User

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


Meaning of an expression

${var} variable var value, same 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 use $default 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 use $default as its value *

${var+other} if Var is declared, then its value is $other, otherwise it will be 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, then print $err_msg *

${var:? ERR_MSG} if Var is not set, then print $err_msg *

${!varprefix*} matches all variables declared at the beginning of Varprefix

${[email protected]} matches all previous variables declared at the beginning of the 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]ost ~]$ var1=11;var2=12;var3=

[[email protected] ~]$ echo ${[email protected]}

Var1 var2 Var3

[[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)


Meaning of an expression

${#string}length of $string

${string:position} in $string, extract the substring from position $position

${string:position:length} in $string, start extracting substrings of length $length from position $position

${string#substring} removes the substring of the shortest match $substring from the beginning of the variable $string

${string# #substring} Remove the substring of the longest matching $substring from the beginning of the variable $string

${string%substring} removes the substring of the shortest match $substring from the end of the variable $string

${string%%substring} Remove the substring of the longest 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 $substring

${string/#substring/replacement} if the $string prefix matches $substring, then $replacement is used instead of the matching $substring

${string/%substring/replacement} if the $string suffix matches $substring, then $replacement is used instead of the matching $substring

Description: "* $substring" can be a regular expression.



1. 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 '

[[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} 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 protected] ~]$ 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 10000);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.


Shell string Processing

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.