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"
#!/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 "/>
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 "/>
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 "/>
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 "/>
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 "/>
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 "/>
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 "/>
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 "/>
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 "/>
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 "/>
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 "/>
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 "/>
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