[Convert] shell string operation method and instance

Source: Internet
Author: User

Each language has its own string operation method, and the shell is the same. The following describes common methods in an example.

 

1. Get the string length

  1. String = abc12342341 // do not have spaces on the second side of the equal sign echo $ {# string} // result 11 expr length $ string // result 11 expr "$ string ":". * "// result 11 there must be a space on the two sides of the semicolon. Here, the root match is similar in usage.

2. string location

  1. Expr index $ string '200' // Result 4 the subscript corresponding to the string starts from 0.

This method reminds me of the JS indexof. The operations on strings in various languages are similar in the general direction. If there is a basic language, learning shell will be very fast.

3. the maximum length from the beginning of the string to the substring

  1. Expr match $ string 'abc. * 3' // result 9

I personally think this function is of little use. Why should it start from the beginning.

4. String Truncation

  1. Echo $ {string: 4} // 2342341 intercepts all the strings following the 4th-Bit String echo $ {string: 3: 3} // 123 capture the next three Echo $ {string: 3: 6} // 3rd capture the Next Six Echo $ {string: -4} // 2341: there is a space on the right side to intercept the last 4 echo $ {string :(-4 )} // 2341 same as expr substr $ string 3 3 // 123 the last three digits are intercepted starting from the first digit

The above method reminds me of the fact that the substr function of PHP is the same as the rules intercepted later.

5. Match the displayed content

  1. // In Example 3, match is also different from match. The length of the matching character is displayed, the following is the matched content expr match $ string '\ ([a-c] * [0-9] * \)' // abc12342341 expr $ string: '\ ([a-c] * [0-9] \)' // ABC1 expr $ string :'. * \ ([0-9] [0-9] [0-9] \) '// 341 display matching content in parentheses
     

Is there any similarity between the brackets in the root and other brackets,

6. Capture unmatched content

  1. Echo $ {string # A * 3} // 42341 starts from the left of $ string, remove the shortest matching substring echo $ {string # C * 3} // abc12342341 and nothing matches echo $ {string # * C1 * 3} // 42341 from the left side of $ string start, remove the shortest matching substring echo $ {string # A * 3} // 41 from the left of $ string, remove the longest matched substring echo $ {string % 3*1} // abc12342 from the right of $ string, remove the shortest matched substring echo $ {string % 3*1} // abc12 from the right of $ string, remove the longest matched substring

Note that it must start with the first character of the string or the last one,

7. Match and replace

  1. Echo $ {string/23/BB} // abc1bb42341 replace echo $ {string // 23/BB} // abc1bb4bb41 double slash replace all matched echo $ {string/# ABC/ bb} // bb12342341 # start with what to match, ^ in the root php is a bit like Echo $ {string/% 41/BB} // What is the end of ECHO $ {string/% 41/BB} // abc123423bb %? $ in the root PHP is a bit like

 

From: http://blog.51yip.com/shell/1080.html

[Convert] shell string operation method and instance

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.