[Switch] The method of shell interception of strings

There are many ways to intercept strings in the shell,

There are 9 ways to use ${expression}.

${parameter:-word}

${parameter:=word}

${parameter:?word}

${parameter:+word} 

The above 4 can be used to replace the default value.

${#parameter}

The above can get the length of the string. 

${parameter%word} minimally intercept word from the back

${parameter%%word} intercept word from the back to the maximum

${parameter#word} minimally intercept word from the front

${parameter##word} intercepts word from the front as much as possible

The above 4 methods are used to intercept strings.

With four usages, there is no need to use the cut command to intercept the string

The first one can be divided into four cases, which are introduced one by one below.

 

1. Use the # operator. The purpose is to delete the first occurrence of the substring from the left, that is, its left character, and keep the right character. The usage is #*substr, for example:

str='http://www.yourdomain.com/cut-string.html'

echo ${str#*//}

The result is www.yourdomain.com/cut-string.html, that is, delete all characters from the left to the first "//" and its left. 2. Use the ## operator. The purpose is to delete the last occurrence of the substring from the left, that is, its left character, and keep the right character. The usage is ##*substr, for example:

str='http://www.yourdomain.com/cut-string.html'

echo ${str##*/}

The result obtained is cut-string.html, which deletes the last occurrence of "/" and all characters to the left

3. Use the % operator. The purpose is to delete the first occurrence of the substring from the right, that is, the right character, and keep the left character. The usage is %substr*, for example:

str='http://www.yourdomain.com/cut-string.html'

echo ${str%/*}

The result is http://www.yourdomain.com, that is, delete all characters from the right to the first "/" and its right

4. Use the %% operator. The purpose is to delete the last occurrence of the substring from the right, that is, its right-hand character, and keep the left-hand character. The usage is %%substr*, for example:

str='http://www.yourdomain.com/cut-string.html'

echo ${str%%/*}

The result is http://www.yourdomain.com, that is, delete all characters from the right to the last "/" and its right

 

The second is also divided into four types, which are described as follows:

1. The first few characters from the left and the number of characters, the usage is: start:len, for example:

str='http://www.yourdomain.com/cut-string.html'

echo $ {var: 0: 5

Where 0 indicates the start of the first character on the left, and 5 indicates the total number of characters.

The result is: http:

2. From the first few characters on the left to the end, the usage is: start, for example:

str='http://www.yourdomain.com/cut-string.html'

echo $ {var: 7

The 7 means the start of the 8th character from the left

The result is: www.yourdomain.com/cut-string.html

3. The first few characters from the right and the number of characters, usage: 0-start:len, for example:

str='http://www.yourdomain.com/cut-string.html'

echo ${str:0-15:10}

Among them, 0-6 means the 6th character from the right, and 10 means the number of characters.

The result is: cut-string

3. From the first few characters on the right to the end, usage: 0-start, for example:

str='http://www.yourdomain.com/cut-string.html'

echo ${str:0-4}

Among them, 0-6 means the 6th character from the right, and 10 means the number of characters.

The result is: html

Note: (the first character on the left is represented by 0, and the first character on the right is represented by 0-1)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326685654&siteId=291194637