Shell string processing command

Source: Internet
Author: User


Shell string processing string length [plain] shell> echo $ {# string} shell> expr length $ string shell> expr "$ string ":'. * 'www.2cto.com matches the length of the substring starting with a string [plain] expr match "$ string"' $ substring is a regular expression. expr "$ string": '$ substring' index [plain] expr index $ string $ substring extract substring [plain] $ {string: position} in $ string, the substring is extracted from position $ position. if $ string is "*" or "@", the parameter starting from position $ position is extracted. [1] $ {string: position: length} www.2cto.com
Extract the length of $ length substring from position $ position in $ string. [plain] stringZ = abcABC123ABCabc #0123456789 ..... #0-based indexing. echo $ {stringZ: 0} # abcABC123ABCabc echo $ {stringZ: 1} # bcABC123ABCabc echo $ {stringZ: 7} #23 ABCabc echo $ {stringZ: 7: 3} # 23A # The length of the extracted substring is 3.
# Can I start to extract the substring from the right (that is, the end) of the string? Echo $ {stringZ:-4} # abcABC123ABCabc # extracts the entire string by default, just like $ {parameter:-default. # however... echo $ {stringZ :(-4) }# Cabc echo $ {stringZ:-4} # Cabc note: ":" There is a space on the right! # In this way, it can work. # use parentheses or add a space to escape the parameter. if the $ string parameter is "*" or "@", $ length parameters are extracted from the $ position parameter, but because $ length parameters may not exist, then several location parameters are extracted. [plain] echo $ {*: 2} # print out 2nd and all the following location parameters. echo $ {@: 2} # Same as above. echo $ {*: 2: 3} # print three position parameters in a row starting from 2nd. www.2cto.com expr substr $ string $ position $ length in $ string, the Child string of $ length is extracted from $ position. [plain] echo 'expr substr $ stringZ 1 2 'echo 'expr substr $ stringZ 4 3 'expr match "$ string" '\ ($ substring \) 'extract $ substring from the starting position of $ string, and $ substring is a regular expression. expr "$ string": '\ ($ substring \)' extracts $ substring from the starting position of $ string, and $ substring is a regular expression.
[Plain] stringZ = abcABC123ABCabc #======= echo 'expr match "$ stringZ "'\(. [B-c] * [A-Z] .. [0-9] \) ''echo 'expr "$ stringZ ":'\(. [B-c] * [A-Z] .. [0-9] \) ''echo 'expr "$ stringZ ":'\(....... \) ''# Each echo above prints the same result. abcABC1 expr match "$ string "'. * \ ($ substring \) 'extracts $ substring from the end of $ string, and $ substring is a regular expression. expr "$ string ":'. * \ ($ substring \) 'extracts $ substring from the end of $ string, and $ substring is a regular expression.
[Plain] expr match "$ stringZ "'. * \ ([A-C] [A-C] [A-C] [a-c] * \) 'abcabc www.2cto.com expr "$ stringZ ":'. *\(...... \) 'abcabc substrings remove [plain] $ {string # substring} and cut off the minimum matching $ substring from the beginning of $ string. $ {string ## substring} removes the longest matched $ substring from the start of $ string. $ {string % substring} Cut the minimum matching $ substring from the end of $ string. $ {string % substring} truncated the longest matched $ substring from the end of $ string. echo $ {stringZ % B * c} # abcABC123ABCa # Cut off the shortest match between 'B' and 'c' from the end of $ stringZ. echo $ {stringZ % B * c} # a # removes the longest match between 'B' and 'c' from the end of $ stringZ. this operation is especially useful when you need to construct a file name. replace www.2cto.com [plain] $ {string/substring/replacement} with $ replacement to replace the first matched $ substring. $ {string // substring/replacement} use $ replacement to replace all matched $ substring. $ {string/# substring/replacement} If $ substring matches the starting part of $ string, $ replacement is used to replace $ substring. $ {string/% substring/replacement} If $ substring matches the end of $ string, $ replacement is used to replace $ substring [extracted from books and processed strings, familiar with the operations provided by Bash, general applications can still handle well]

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.