Shell string interception
1, taken from the specified start location
2904628156
This approach requires two parameters: 1. the length taken starting position 2.
Since the need to specify the starting position, then issues related to the counting direction, in the end character string is counted from the left or right side counted from the string, The answer is that shell support both counting methods
Counting from the left of the string 1.1
Counted from the left, taken as format string ${string:start:length}
, string is the string to be taken, start is the starting position (starting from the left, starts counting from 0), the length is taken to be the length (not string until it indicates end)
For example:
#! /bin/bash url="http://www.baidu.com" echo ${url:5:11}
Results//www.baidu
Another example:
#! /bin/bash url="http://www.baidu.com" echo $ {url: 5} # omitted length, taken to the end of the string
2.1 counting from the right
Counted from the right, taken in the string format
$ {string: 0-start: length}
with comparison counting from the left, only the format of the multi-0-, which is fixed wording, designed to identify the character counting the right string
Note the following two points:
- When counted from the left, starting number is 0; when counted from the right, the starting number is 1
- Regardless of which side count, taken from left to right direction are
E.g:
#! /bin/bash url="http://www.baidu.com" echo ${url:0-9:5}
Results baidu
. From the right, b
is the ninth character
Another example:
#! /bin/bash url="http://www.baidu.com" echo $ {url: 0-9} # omitted length, taken directly to the end of the string
Resultsbaidu.com
2, 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.
2.1 Use the # character to the right of interception
Can be taken using the # specified character (a character string) to the right of all the characters, the following format:
$ # * {String} chars
wherein, string represents a string to be intercepted, chars are specified character (or sub-string), * is a wildcard string representing any length, * chars understood as all characters up even ignore the left side, until it encounters chars (chars will not be intercepted)
For example:
#! /bin/bash url="http://www.baidu.com" echo ${url#*://}
Resultswww.baidu.com
If you do not ignore the character chars left, then you can not write *
, for example,
#! /bin/bash url="http://www.baidu.com" echo ${url#http://}
Resultswww.baidu.com
Note: The above written characters encountered the first matching (substring) is over.
E.g
#! /bin/bash url="http://www.baidu.com/index.html" echo ${url#*/}
Results /www.baidu.com/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}
1, taken from the specified start location
2904628156
This approach requires two parameters: 1. the length taken starting position 2.
Since the need to specify the starting position, then issues related to the counting direction, in the end character string is counted from the left or right side counted from the string, The answer is that shell support both counting methods
Counting from the left of the string 1.1
Counted from the left, taken as format string ${string:start:length}
, string is the string to be taken, start is the starting position (starting from the left, starts counting from 0), the length is taken to be the length (not string until it indicates end)
For example:
#! /bin/bash url="http://www.baidu.com" echo ${url:5:11}
Results//www.baidu
Another example:
#! /bin/bash url="http://www.baidu.com" echo $ {url: 5} # omitted length, taken to the end of the string
2.1 counting from the right
Counted from the right, taken in the string format
$ {string: 0-start: length}
with comparison counting from the left, only the format of the multi-0-, which is fixed wording, designed to identify the character counting the right string
Note the following two points:
- When counted from the left, starting number is 0; when counted from the right, the starting number is 1
- Regardless of which side count, taken from left to right direction are
E.g:
#! /bin/bash url="http://www.baidu.com" echo ${url:0-9:5}
Results baidu
. From the right, b
is the ninth character
Another example:
#! /bin/bash url="http://www.baidu.com" echo $ {url: 0-9} # omitted length, taken directly to the end of the string
Resultsbaidu.com
2, 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.
2.1 Use the # character to the right of interception
Can be taken using the # specified character (a character string) to the right of all the characters, the following format:
$ # * {String} chars
wherein, string represents a string to be intercepted, chars are specified character (or sub-string), * is a wildcard string representing any length, * chars understood as all characters up even ignore the left side, until it encounters chars (chars will not be intercepted)
For example:
#! /bin/bash url="http://www.baidu.com" echo ${url#*://}
Resultswww.baidu.com
If you do not ignore the character chars left, then you can not write *
, for example,
#! /bin/bash url="http://www.baidu.com" echo ${url#http://}
Resultswww.baidu.com
Note: The above written characters encountered the first matching (substring) is over.
E.g
#! /bin/bash url="http://www.baidu.com/index.html" echo ${url#*/}
Results /www.baidu.com/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}