There are usually two ways to intercept strings in Shell: intercept from the specified position and intercept from the specified character (substring).
Start intercepting from the specified location
This method requires two parameters: in addition to specifying the starting position, it also needs to intercept the length to finally determine the string to be intercepted.
Since you need to specify the starting position, it involves the counting direction. Should you start counting from the left side of the string or from the right side of the string? The answer is that Shell supports two counting methods at the same time.
1) Start counting from the left side of the string
If you want to start counting from the left side of the string, the specific format of intercepting the string is as follows:
${string: start :length}
where string is the string to be intercepted, start is the starting position (starting from the left and counting from 0), and length is the length to be intercepted (if omitted, it means until the end of the string).
For example:
url="" echo ${url: 2: 9}
The result is biancheng.
Another example:
url="" echo ${url: 2} #Omit length and intercept the end of the string
The result is.
2) Start counting from the right
If you want to start counting from the right side of the string, the specific format of intercepting the string is as follows:
${string: 0-start :length}
Compared with the first) format, the second) format only has 0- more, which is a fixed writing method, specifically used to indicate counting starting from the right side of the string.
Two points need to be emphasized here:
- When counting from the left, the starting number is 0 (this is in line with programmer's thinking); when counting from the right, the starting number is 1 (this is in line with ordinary people's thinking). The counting direction is different, and the starting number is also different.
- No matter which side you start counting, the interception direction is from left to right.
For example:
url="" echo ${url: 0-13: 9}
The result is biancheng. From the right, b is the 13th character.
Another example:
url="" echo ${url: 0-13} #Omit length and directly intercept the end of the string
The result is.
Intercept from the specified character (substring)
This interception method cannot specify the string length, and can only be intercepted from the specified character (substring) to the end of the string. Shell can intercept all characters on the right side of the specified character (substring), or all characters on the left.
1) Use the # number to intercept the right character
Use the # number to intercept all characters on the right side of the specified character (or substring). The specific format is as follows:
${string#*chars}
where string represents the character to be intercepted, chars is the specified character (or substring), and * is a wildcard character that represents a string of any length. *Cars are used together to mean: ignore all characters on the left until they meet chars (chars will not be intercepted).
Please see the following example:
url="/" echo ${url#*:}
The result is ///.
The following writing method can also get the same result:
echo ${url#*p:} echo ${url#*ttp:}
If you don't need to ignore the characters on the left of chars, you can also not write *, for example:
url="/" echo ${url#http://}
The result is /.
Note that the above writing method ends when it encounters the first matching character (substring). For example:
url="/" echo ${url#*/}
The result is //. There are three / in the url string, and the output results show that the match ends when the Shell encounters the first /.
If you want to match until the last specified character (substring) is finished, you can use ##, in the specific format:
${string##*chars}
Please see the following example:
#!/bin/bash url="/" echo ${url#*/} #The result is //echo ${url##*/} #The result isstr="---aa+++aa@@@" echo ${str#*aa} #The result is +++aa@@@echo ${str##*aa} #The result is @@@
2) Use % to intercept the left character
Use the % number to intercept all characters on the left side of the specified character (or substring). The specific format is as follows:
${string%chars*}
Note the position of *, because you want to intercept the characters on the left of chars and ignore the characters on the right of chars, so * should be on the right of chars. The usage of % and # in other aspects is the same. I will not repeat it here, just give examples:
#!/bin/bash url="/" echo ${url%/*} #The result isecho ${url%%/*} #The result is http:str="---aa+++aa@@@" echo ${str%aa*} #The result is --aa+++echo ${str%%aa*} #The result is ---
Summary
Finally, we will summarize the above 8 formats, please see the following table:
Format | illustrate |
---|---|
${string: start :length} | Start from the start character on the left of the string string and intercept the length character to the right. |
${string: start} | Start from the start character on the left side of the string string until the end. |
${string: 0-start :length} | Start from the start character on the right of the string string and intercept the length character to the right. |
${string: 0-start} | Start from the start character on the right side of the string string until the end. |
${string#*chars} | Starting from the position where *chars first appears in the string string, intercept all characters to the right of *chars. |
${string##*chars} | Starting from the position where *chars last appears in the string string, intercept all characters to the right of *chars. |
${string%*chars} | Starting from the position where *chars first appears in the string string, intercept all characters to the left of *chars. |
${string%%*chars} | Starting from the position where *chars last appears in the string string, intercept all characters to the left of *chars. |
This is the end of this article about the implementation method of Shell string interception (very detailed). For more related Shell string interception content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!