Shell scripts--string interception

Source: Internet
Author: User
Tags truncated

If you want to intercept a string, you can do it by manipulating the subscript or pointer in C + +, and in a shell script, there are several ways to do this if you want to specify a string for interception:


The following examples are used:str= "hi/i/am/just/a/string"


    • ${str#*char} indicates that all characters to the left of the first char are deleted from the left , and the right string is truncated

#!/bin/bashstr=hi/i/am/just/a/stringecho ${str}cut=${str#*/}echo ${cut}

In fact * can be regarded as a wildcard character, that is, all characters to the left of Char are deleted;

To run the program:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/82/E8/wKiom1dj6fTC34OVAAAFc2Ycjco761.png "style=" float: none; "title=" #.png "alt=" Wkiom1dj6ftc34ovaaafc2ycjco761.png "/>


    • ${str##*char} indicates that all characters to the left of the last char are deleted from the left , capturing the right string

Replace the example program with the following:

cut=${str##*/}

To run the program:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/82/E7/wKioL1dj6ffiHY07AAAFY0NCidc596.png "style=" float: none; "title=" ##.png "alt=" Wkiol1dj6ffihy07aaafy0ncidc596.png "/>


    • ${str%char*} indicates that all characters to the right of the first char are deleted from the right , and the left string is truncated

Replace the example program with the following:

cut=${str%/*}

The same * can be seen as a wildcard character, indicating that all characters on the right are deleted, leaving the left side;

To run the program:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/82/E7/wKioL1dj6fezX0DHAAAFXZ7C8Go773.png "style=" float: none; "title="%.png "alt=" Wkiol1dj6fezx0dhaaafxz7c8go773.png "/>


    • ${str%char*} indicates that all characters to the right of the last char are deleted from the right , capturing the left string

Replace the example procedure as follows:

cut=${str%%/*}

To run the program:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/82/E8/wKiom1dj6ffwbmZ6AAAFVp9WobE800.png "style=" float: none; "title="%%.png "alt=" Wkiom1dj6ffwbmz6aaafvp9wobe800.png "/>


    • ${str:n:m} means to intercept M characters starting from the nth character on the left , excluding the nth character

Replace the example program with the following:

Cut=${str:3:4}

Represents the next 4 characters starting from the 3rd character on the left, or 0 for the first character , but including the first character;

To run the program:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/82/E8/wKiom1dj6fjitxkBAAAFYHUIBoU893.png "style=" float: none; "Title=" 3 than 4.png "alt=" Wkiom1dj6fjitxkbaaafyhuibou893.png "/>


    • ${str:n} indicates that all subsequent characters are truncated from the nth character on the left , excluding the nth character

Change the above procedure to read as follows:

Cut=${str:7}

Represents all characters starting at the left of the 7th character, whichever follows;

To run the program:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/82/E7/wKioL1dj6fjTVLxbAAAFNmWSI7k386.png "style=" float: none; "title=" 7 than. png "alt=" Wkiol1dj6fjtvlxbaaafnmwsi7k386.png "/>


    • ${str:0-n:m} means to intercept M characters starting from the nth character on the right , including the nth character

Replace the example program with the following:

Cut=${str:0-7:3}

Starts at the 7th character starting at the right, including the seventh character itself, and intercepts three characters;

To run the program:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/82/E7/wKioL1dj6f6iVhQpAAATHRWw1Zc156.png "style=" float: none; "Title=" right 3 than 4.png "alt=" Wkiol1dj6f6ivhqpaaathrww1zc156.png "/>


    • ${str:0-n} represents all characters following the beginning of the nth character on the right , including the nth character

Replace the example program with the following:

CUT=${STR:0-7}

Represents the beginning of the 7th character from the right to intercept all subsequent characters;

To run the program:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/82/E8/wKiom1dj6gOx02FuAAAFQY3xJ_A315.png "style=" float: none; "Title=" 0-7 than. png "alt=" Wkiom1dj6gox02fuaaafqy3xj_a315.png "/>


    • $ {STR} | cut-c n indicates the nth character to be taken from the left-hand number

Example code:

#!/bin/bashstr=hi/i/am/just/a/stringecho ${str}echo ${str} | Cut-c 7

Represents the interception of the 7th character from the left;

To run the program:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/82/E9/wKiom1dj8EPDoiyEAAAQ8mvTnAI695.png "style=" float: none; "title=" N.png "alt=" Wkiom1dj8epdoiyeaaaq8mvtnai695.png "/>


    • $ {STR} | cut-c n = intercepts all characters from the beginning of the nth character in the left-hand number, including nth

Change the program as above:

Echo ${str} | Cut-c 7-

Represents all the remaining characters starting from the Intercept from the 7th character on the left;

To run the program:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/82/E7/wKioL1dj8FWDOWNCAAARmR8U6Ic639.png "style=" float: none; "title=" N-.png "alt=" Wkiol1dj8fwdowncaaarmr8u6ic639.png "/>


    • $ {STR} | cut-c n-m represents the interception of all characters from the nth character to the first m character, including the nth character, but excluding the first m characters from the left

Change the procedure to read as follows:

Echo ${str} | Cut-c 2-7

Represents the interception of all characters from the left 2nd character to the 7th character, including the 2nd character, excluding the 7th character;

To run the program:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/82/E9/wKiom1dj8FrBgbc2AAAQZczm7aY516.png "style=" float: none; "title=" N-m.png "alt=" Wkiom1dj8frbgbc2aaaqzczm7ay516.png "/>


    • $ {STR} | cut-c -n means to intercept all characters going forward, including the nth character, from the beginning of the left

Change the procedure to read as follows:

Echo ${str} | Cut-c-7

Represents the first number of characters from the left of the 7th character to delete all characters, and the preceding characters are truncated;

Run the program as follows:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/82/E7/wKioL1dj8F2CMUDzAAAQKKRWAGc036.png "style=" float: none; "title="-n.png "alt=" Wkiol1dj8f2cmudzaaaqkkrwagc036.png "/>


    • expr substr "${str}" N M indicates that m characters are truncated from the nth character on the left

Sample program:

#!/bin/bashstr=hi/i/am/just/a/stringecho ${str}cut=$ (Expr substr "${str}" 3 5) Echo ${cut}

Because expr is a command, the above $ () is the command substitution, which means that the intercept starts from the 3rd character on the left and intercepts 5 characters;

To run the program:

650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M02/82/E7/wKioL1dj9VHwwjLrAAARjR9qvzk950.png "title=" expr Substr.png "alt=" Wkiol1dj9vhwwjlraaarjr9qvzk950.png "/>



Finish

This article is from the "Knock Code good Sleep zzz" blog, please be sure to keep this source http://2627lounuo.blog.51cto.com/10696599/1790446

Shell scripts--string interception

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.