Shell string interception (very detailed)

 

Shell  interception string usually in two ways: Start intercepted and taken from the specified start character (substring) from the specified location.

Taken from the specified start location

This approach requires two parameters: In addition to specifying the starting position, but also need to intercept length, to be taken to finalize the string.

Now you need to specify the starting position, then the issues related to the counting direction, in the end is the beginning of a string counting from the left or from the right strings begin counting. The answer is that Shell supports both counting methods.

1) counting from the left of the string

If you want to start counting from the left of the string, then the string is taken as the specific format:

${string: start :length}

Here, string is the string to be intercepted, start is the starting position (starting from the left, starts counting from 0), the length is taken to be the length (not until then represents the end of the string).

E.g:

  1. url="c.biancheng.net"
  2. echo ${url: 2: 9}

Results biancheng.

Another example:

  1. url="c.biancheng.net"
  2. echo $ { URL : 2 } # omitted length, taken to the end of the string

Results biancheng.net.

2) counting from the right

If you want to start counting from the right of the string, then the string is taken as the specific format:

${string: 0-start :length}

Compared with the first) formats, 2) more formats only 0-, which is fixed wording, designed to indicate the start counting from the right string.

It should be emphasized two points:

  • When counted from the left, starting number is 0 (This is consistent thinking the programmer); the right from the beginning when the count start number is 1 (This is consistent with ordinary thinking). Different counting directions, starting number are different.
  • Regardless of which side starts counting directions are taken from left to right.


E.g:

  1. url="c.biancheng.net"
  2. echo ${url: 0-13: 9}

Results biancheng. From the right, bit is the first 13 characters.

Another example:

  1. url="c.biancheng.net"
  2. echo $ { URL : 0-13 } # omitted length, taken directly to the end of the string

Results biancheng.net.

Taken from the specified character (substring) Start

This method can not intercept length of the string, can be taken from the specified character (a character string) to the end of the string. Shell can intercept specified character (substring) of all characters to the right, you can also intercept all the characters to the left.

1) Use the # character to the right of interception

Use #numbers can intercept the specified character (or substring) of all characters to the right, the following format:

${string#*chars}

Here, string representing the character to be intercepted, chars are specified character (or sub-string) *is a wildcard, represents an arbitrary length string. *charsTo link the use of means: ignore all characters to the left, until I met chars (chars will not be intercepted).

Consider the following example:

  1. url="http://c.biancheng.net/index.html"
  2. echo ${url#*:}

Results //c.biancheng.net/index.html.

The following wording can also get the same result:

  1. echo ${url#*p:}
  2. echo ${url#*ttp:}


If you do not ignore the character chars left, then you can not write *, for example:

  1. url="http://c.biancheng.net/index.html"
  2. echo ${url#http://}

Results c.biancheng.net/index.html.

Note that the above written characters encountered the first matching (substring) is over. E.g:

  1. url="http://c.biancheng.net/index.html"
  2. echo ${url#*/}

Results /c.biancheng.net/index.html. url string has three /output results show, Shell met first /on the match ended.

If you want to last until a specified character (substring) and then match the end, you can use ##the specific format:

${string##*chars}

Consider the following example:

  1. #!/bin/bash
  2. url="http://c.biancheng.net/index.html"
  3. echo $ {# URL * /} # result /c.biancheng.net/index.html
  4. echo $ {URL ## * /} # result index.html
  5. str="---aa+++aa@@@"
  6. echo $ AA *} {# STR # results +++ aa @@@
  7. echo $ STR {##} * AA # @@@ result

2) using the% character left interception

Use %numbers can intercept the specified character (or substring) all the characters to the left, the following format:

${string%chars*}

Please note *the location, because the character chars left to be intercepted, and the character to ignore the right of the chars, it *should be located on the right side of chars. Other aspects %and #the use of the same, is not repeated here, way of example only:

  1. #!/bin/bash
  2. url="http://c.biancheng.net/index.html"
  3. echo $% URL {/ *} # result http://c.biancheng.net
  4. echo $ %% URL {/ *} # results http:
  5. str="---aa+++aa@@@"
  6. echo $ STR% AA *} { # result --- aa +++
  7. echo $ %% STR AA *} { # result ---

Gather

Finally, we make a summary of more than 8 formats, see the following table:

format Explanation
${string: start :length} 从 string 字符串的左边第 start 个字符开始,向右截取 length 个字符。
${string: start} 从 string 字符串的左边第 start 个字符开始截取,直到最后。
${string: 0-start :length} 从 string 字符串的右边第 start 个字符开始,向右截取 length 个字符。
${string: 0-start} 从 string 字符串的右边第 start 个字符开始截取,直到最后。
${string#*chars} 从 string 字符串第一次出现 *chars 的位置开始,截取 *chars 右边的所有字符。
${string##*chars} 从 string 字符串最后一次出现 *chars 的位置开始,截取 *chars 右边的所有字符。
${string%*chars} 从 string 字符串第一次出现 *chars 的位置开始,截取 *chars 左边的所有字符。
${string%%*chars} 从 string 字符串最后一次出现 *chars 的位置开始,截取 *chars 左边的所有字符。

Guess you like

Origin www.cnblogs.com/cangqinglang/p/11952838.html