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

Source: Internet
Author: User

Linux Shell String Operations (length, find, replace, match) 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]localhost ~]$ 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 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)

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 .

1. Length

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

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

Another way:

Expr Length "$adcode"

2. Intercept strings

[Email protected] ~]$ test= ' I love China '
[[email protected] ~]$ echo ${test:5}
E China
[[email protected] ~]$ echo ${test:5:10}
E China

echo ${test: (-3)}

Ina

echo ${test: (-3): 2}

Inch

${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=$ (expr length $test);d one;       

real    0m9.734s
user    0m1.628s

  The

has a hundredfold speed difference, calling external command processing, which differs greatly from the built-in operator performance. In shell programming, try to complete with built-in operators or functions. Similar results can occur with awk,sed.

 

four: Match

If [  ' echo $str | grep-e regexp '  ];the N ...

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.