Brief Analysis of Operation strings

Source: Internet
Author: User

An analysis of the number of string operations supported by Bash is surprising. unfortunately, these tools lack unified standards. some are subset of parameter replacement, while others are affected by the UNIX expr command. this leads to inconsistent command syntax and redundant functions, but these are not confusing. method to obtain the string length: 1 $ {# string} 2 expr length $ string 3 expr "$ string ":'. * 'root @ ubuntu :~ /Resource/shell-study/0507-2013 # string = "abcdefg" root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo $ {# string} 7 root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo 'expr length $ string' 7 root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo 'expr "$ string": '. *' 7 root @ ubuntu :~ /Resource/shell-study/0507-2013 # How to learn: insert blank lines between paragraphs in a text file #! /Bin/bash MINLEN = 3 while read line do echo "$ line" len =$ {# line} if ["$ len"-lt "$ MINLEN"]; then echo fi done exit 0 result: root @ ubuntu :~ /Resource/shell-study/0507-2013 #./test6.sh <file.txt 12345 12 121321313 12 131234 2 123424155 3rwq e wer weee e eeeerqwte root @ ubuntu :~ /Resource/shell-study/0507-2013 # Method 1: expr match "$ string" '$ substring is a regular expression that matches the length of a substring starting with a string. method 2: expr "$ string": '$ substring' $ substring is a regular expression. index expr index $ string $ substring the position where $ substring matches in the string $ string for the first time. root @ ubuntu :~ /Resource/shell-study/0507-2013 # string = abcABC123ABCabc root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo 'expr index "$ string" C12 '6 root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo 'expr index "$ string" A' 4 root @ ubuntu :~ /Resource/shell-study/0507-2013 # Method for Extracting substrings $ {string: position} in $ string, extract substrings from position $ position, if $ string is "*" or "@", the parameter $ {string: position starting from position $ position is extracted: length} in $ string, extract the substring of $ length from position $ position. root @ ubuntu :~ /Resource/shell-study/0507-2013 # string = "abcABC123ABCabc" root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo $ {string: 0} abcABC123ABCabc root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo $ {string: 2} cABC123ABCabc root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo $ {string: 8} 3 ABCabc root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo $ {string: 8: 3} 3AB root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo $ {string:-4} abcABC123ABCabc root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo $ {string :(-4)} Cabc root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo $ {string:-4} Cabc root @ ubuntu :~ /Resource/shell-study/0507-2013 # the entire string is extracted by default, just like $ {parameter:-default}, so the last and third commands return the entire string, however, you can use parentheses or add a space to "escape" this location parameter. The consciousness is to print the last four characters. If the $ string parameter is "*" or "@", $ length parameters are extracted from the $ position parameter. However, since there may be no $ length parameters, several location parameters are extracted. Method 2: expr substr $ string $ position $ length in $ string, extract the $ length substring from $ position. root @ ubuntu :~ /Resource/shell-study/0507-2013 # string = "abcABC123ABCabc" root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo 'expr substr $ string 1 2' AB root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo 'expr substr $ string 1 4' abcA root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo 'expr substr $ string 4 3 'ABC root @ ubuntu :~ /Resource/shell-study/0507-2013 # method 3: expr match "$ string" '\ ($ substring \)' extract $ substring from the starting position of $ string, $ substring is a regular expression. expr "$ string": '\ ($ substring \)' extracts $ substring from the starting position of $ string, and $ substring is a regular expression. root @ ubuntu :~ /Resource/shell-study/0507-2013 # string = "abcABC123ABCabc" root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo 'expr match "$ string "'\(. [B-c] * [A-Z] .. [0-9] \) ''abcabc1 root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo 'expr "$ string ":'\(. [B-c] * [A-Z] .. [0-9] \) ''abcabc1 root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo 'expr "$ string": '\ (... \)' 'abcab root @ ubuntu :~ /Resource/shell-study/0507-2013 # Method 4: 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. root @ ubuntu :~ /Resource/shell-study/0507-2013 # string = "abcABC123ABCabc" root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo 'expr match "$ string "'. * \ ([A-C] [A-C] [A-C] [a-c] * \) ''ABCabc root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo 'expr "$ string ":'. * \ ([A-C] [A-C] [A-C] [a-c] * \) ''ABCabc root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo 'expr "$ string": '. * \ (... \)' BCabc root @ ubuntu :~ /Resource/shell-study/0507-2013 # method 1 of substring pruning: $ {string # substring} Cut the minimum matching $ substring from the beginning of $ string. $ {string ## substring} removes the longest matching $ substrinroot @ ubuntu: ~ from the beginning of $ string :~ /Resource/shell-study/0507-2013 # string = "abcABC123ABCabc" root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo $ {string # a * c} ABC123ABCabc root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo $ {string # a * C} 123 ABCabc root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo $ {string # a * C} abc root @ ubuntu :~ /Resource/shell-study/0507-2013 # Method 2: $ {string % substring} Cut the minimum matching $ substring from the end of $ string. $ {string % substring} truncated the longest matched $ substring from the end of $ string. root @ ubuntu :~ /Resource/shell-study/0507-2013 # string = "abcABC123ABCabc" root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo $ {string % B * c} abcABC123ABCa root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo $ {string % B * c} a root @ ubuntu :~ /Resource/shell-study/0507-2013 # method 1 of substring replacement: $ {string/substring/replacement} use $ replacement to replace the first matched $ substring. $ {string // substring/replacement} use $ replacement to replace all matched $ substring. root @ ubuntu :~ /Resource/shell-study/0507-2013 # string = "abcABC123ABCabc" root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo $ {string/abc/xyz} xyzABC123ABCabc root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo $ {string // abc/xyz} xyzABC123ABCxyz root @ ubuntu :~ /Resource/shell-study/0507-2013 # Method 2: $ {string/# substring/replacement} If $ substring matches the beginning of $ string, replace $ substring with $ replacement. $ {string/% substring/replacement} If $ substring matches the end of $ string, $ replacement is used to replace $ substring. this method only processes the starting part or the ending part, and does not have any effect on the intermediate character. root @ ubuntu :~ /Resource/shell-study/0507-2013 # string = "abcABC123ABCabc" root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo $ {string/# abc/XYZ} XYZABC123ABCabc root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo $ {string/% abc/XYZ} abcABC123ABCXYZ root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo $ {string/% ABC/XYZ} abcABC123ABCabc root @ ubuntu :~ /Resource/shell-study/0507-2013 # echo $ {string/# ABC/XYZ} abcABC123ABCabc root @ ubuntu :~ /Resource/shell-study/0507-2013 #

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.