Multiple methods for string interception in shell scripts

Source: Internet
Author: User

1. #号截取, remove the left character and leave the right character.     

Code: Echo ${var#*//}

where Var is the variable name, #是运算符, *//indicates that the first//and all characters on the left are deleted from the left.    

Example:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/83/87/wKiom1d1FsDzeXPlAAAkuhf70v0980.png "title=" Str1.1.png "alt=" Wkiom1d1fsdzexplaaakuhf70v0980.png "/>

Results:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/83/85/wKioL1d1FtKCNfcvAAAbGnoHuHw096.png "title=" Str1.2.png "alt=" Wkiol1d1ftkcnfcvaaabgnohuhw096.png "/>


2.# #截取, remove left character, leave right character

Code: Echo ${var##*/}

Where ##*/indicates that the last (rightmost) one/number and all characters to the left are deleted from the left.

Example:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/83/85/wKioL1d1F0-j5jSQAAAkWjWuxEE672.png "title=" Str2.1.png "alt=" Wkiol1d1f0-j5jsqaaakwjwuxee672.png "/>

Results:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/83/85/wKioL1d1FxvTSeWfAAATSoV__qA698.png "title=" Str2.2.png "alt=" Wkiol1d1fxvtsewfaaatsov__qa698.png "/>

3.%, delete the right character, leave the left character

Code: Echo ${var%/*}

where%/* means to start from the right, delete the first/second and right character

Example:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/83/87/wKiom1d1F3OyGvwQAAAfiw07HIw095.png "title=" Str3.1.png "alt=" Wkiom1d1f3oygvwqaaafiw07hiw095.png "/>

Results:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/83/85/wKioL1d1F4_B0FdrAAAR14_VFpQ326.png "title=" Str3.2.png "alt=" Wkiol1d1f4_b0fdraaar14_vfpq326.png "/>

4.%%, delete the right character, leave the left character

Code: Echo ${%%/*}

Where%%/* means to delete the last (leftmost) one/and right character from the right

Example:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/83/87/wKiom1d1F7zgIq6aAAAi3YD4qSs128.png "title=" Str4.1.png "alt=" Wkiom1d1f7zgiq6aaaai3yd4qss128.png "/>

Results:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/83/85/wKioL1d1F9SzSUMkAAASARyFREk192.png "title=" Str4.2.png "alt=" Wkiol1d1f9szsumkaaasaryfrek192.png "/>

5. Start with the first few characters on the left and the number of characters

Code: Echo ${var:0:5}

Where 0 means the first character on the left begins, and 5 indicates the total number of characters

Example:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/83/85/wKioL1d1F_LxOI4CAAAfQX4E6Kw673.png "title=" Str5.1.png "alt=" Wkiol1d1f_lxoi4caaafqx4e6kw673.png "/>

Results:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/83/87/wKiom1d1GAvw-9DzAAAR3ahksnc030.png "title=" Str5.2.png "alt=" Wkiom1d1gavw-9dzaaar3ahksnc030.png "/>

6. Starting from the first few characters on the left, until the end

Code: Echo ${var:7}

7 of these means starting with the 8th character on the left, until the end

Example:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/83/87/wKiom1d1GE_Ac3DBAAAegVxfewY289.png "title=" Str6.1.png "alt=" Wkiom1d1ge_ac3dbaaaegvxfewy289.png "/>

Results:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/83/85/wKioL1d1GGXg_ZeOAAAR4UFJcYQ920.png "title=" Str6.2.png "alt=" Wkiol1d1ggxg_zeoaaar4ufjcyq920.png "/>

7. Start with the first few characters on the right and the number of characters

Code: Echo ${var:0-7:3}

Where 0-7 means starting at the seventh character from the right, and 3 representing the number of characters

Example:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/83/85/wKioL1d1GISDMwMGAAAgxRgV-TA690.png "title=" Str7.1.png "alt=" Wkiol1d1gisdmwmgaaagxrgv-ta690.png "/>

Results:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/83/87/wKiom1d1GJrDTl-jAAARGj_2i5Y592.png "title=" Str7.2.png "alt=" Wkiom1d1gjrdtl-jaaargj_2i5y592.png "/>

8. Start from the first few characters on the right until the end

Code: Echo ${var:0-7}

Starts at the seventh character on the right, until the end

Example:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/83/85/wKioL1d1GNCieDBkAAAkJ6UW224077.png "title=" Str8.1.png "alt=" Wkiol1d1gnciedbkaaakj6uw224077.png "/>

Results:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/83/87/wKiom1d1GOTzdq-DAAAVINH68Ps927.png "title=" Str8.2.png "alt=" Wkiom1d1gotzdq-daaavinh68ps927.png "/>

The 9.cut command mainly accepts three positioning methods:

(1) Bytes, with option-B

(2) Character, with option-C

(3) Domain, with option-f

Example:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/83/87/wKiom1d1HLSgJ4OIAABNiwg3THI043.png "title=" Cut1.png "alt=" Wkiom1d1hlsgj4oiaabniwg3thi043.png "/>

So it seems that the-B and-C options are no different, it is not, because the WHO output are single-byte characters, so with-B and c no difference, if changed to Chinese, then only C can be used, and-B output is garbled.

We mentioned that-B and-C can only extract information in a fixed-format document, and for non-fixed format information is useless, then the "domain" is used.

Example:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/83/87/wKiom1d1HzDBZwTMAACNN0iFyA0689.png "title=" Cut2.png "alt=" Wkiom1d1hzdbzwtmaacnn0ifya0689.png "/>

This article is from the "Zwy" blog, make sure to keep this source http://10548195.blog.51cto.com/10538195/1794726

Multiple methods for string interception in shell scripts

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.