Linuxshellsubstr (split string)

Source: Internet
Author: User
Linuxshellsubstr (split string) is originally implemented by calling substr in awk if the string is to be intercepted in shell. Today I learned a new method, which is supported by shell itself. For example, there is a string of 12345678 & amp; Prime;, which needs to be intercepted now... in linux shell substr (split string), if a string is intercepted in shell, substr in awk is called. Today I learned a new method, which is supported by shell itself. For example, if there is a string "12345678", you need to extract the string range of the third to sixth characters. You can: [xok. la ~] $ Export str = "123456789" [xok. la ~] $ Echo $ {str: 3 :( 6-3)} 456 [xok. la ~] $ Unset str can be seen from the second expression above, that is, $ {str: 3 :( 6-3)} can be summarized as: $ {str: begin: len }, and supports expressions, such as 6-3. I have found some other usage on the Internet: shell string truncation problem: 1. Linux shell intercepts the first eight digits of the character variable by using the following methods: 1. expr substr "$ a" 1 82. echo $ a | awk '{print substr (,)}' 3. echo $ a | cut-c1-84.echo $5. expr $ :'\(. \\). * '6. echo $ a | dd bs = 1 count = 8 2>/dev/null 2. truncate by specified string 1. Method 1: $ {varible # * string} captures the string behind the last string from left to right $ {varible # * string} captures the string behind the first string from left to right $ {varible % string *} truncates the string $ {varible % string *} after the last string from the right to the left to extract the string "*" after the first string from the right to the left. Example: $ myvar1_foodforthought.jpg $ echo $ {MYVAR # * fow.rthought.jpg $ echo $ {MYVAR # * fow.odforthought.jpg 2. Method 2: $ {varible: n1: n2 }: truncates the string from n1 to n2 of the Varble variable. You can use another form of variable extension to select a special character string based on the specific character offset and length. Try entering the following line in bash: $ EXCLAIM = cowabunga $ echo $ {EXCLAIM: 0: 3} cow $ echo $ {EXCLAIM: 3: 7} abunga string truncation is very simple. you only need to use a colon to separate the Start character and the length of the substring. 3. split according to specified requirements: for example, get the suffix ls-al | cut-d "."-f2
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.