Linuxshell string operations (length, search, replacement)

Source: Internet
Author: User
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 starting external programs...

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 wait time for external programs to start,
Therefore, the speed will be very fast.
 
I. judge to read the string value
Expression meaning
The value of the variable var $ {var} 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 = DEAULT} 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 role 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 *} matches all variables declared starting with varprefix.
$ {! Varprefix @} matches all variables declared starting with varprefix.
 
2. string operations (length, read and replace)
$ {# String} $ string length
$ {String: position} in $ string, the substring is extracted from the position $ position.
$ {String: position: length} in $ string, the substring with the length of $ length is extracted from the position $ position.
Starting with $ string, $ {string # substring} deletes the substring that matches the minimum $ substring value.
$ {String ## substring} starts with the variable $ string and deletes the substrings that match the longest $ substring.
$ {String % substring}: removes the substring that matches the minimum $ substring value from the end of $ string.
$ {String % substring}: removes the substring that matches $ substring at the end of $ string.
$ {String/substring/replacement} uses $ replacement to replace the first matched $ substring.
$ {String // substring/replacement} uses $ replacement instead of all matched $ substring
$ {String/# substring/replacement} if the $ string prefix matches $ substring, $ replacement is used to replace the matched $ substring.
$ {String/% substring/replacement} if the $ string suffix matches $ substring, $ replacement is used to replace the matched $ substring.

This article is from "Zhang Libin"
 

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.