Home >php教程 >PHP开发 >Summary of shell interception string methods in Linux

Summary of shell interception string methods in Linux

高洛峰
高洛峰Original
2016-11-16 11:56:411400browse

There are many ways to intercept strings in shell,

${expression} has a total of 9 ways to use it.

${parameter:-word}

${parameter:=word}

${parameter:?word}

${parameter:+word}

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

${#parameter}

The above method can get the length of the string.

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

${parameter%%word} Maximize the interception of the word from the back

${parameter#word} Minimize the interception of the word from the front

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

The above 4 methods are used to intercept strings.

With four usages, you don’t have to use the cut command to intercept strings

The first one can be divided into four situations, 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, the characters on the left, and retain the characters on the right. The usage is #*substr, for example:

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

echo ${str#*//}

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

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

echo ${str##*/}

The result is cut -string.html, that is, delete the last appearing "/" and all characters to its left

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

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

echo ${str%/*}

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

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

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

echo ${str%%/*}

The result is http ://www.your domain name.com, that is, delete the characters from the right to the last "/" and all the characters on the right. The second type is also divided into four types, which are introduced as follows:

1. Which number from the left? The starting character and the number of characters are: start:len, for example:

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

echo ${var:0: 5}

The 0 represents the beginning of the first character on the left, and the 5 represents the total number of characters.

The result is: http:

2. From the left character to the end, the usage is: start, for example:

str='http://www.your domain name.com/cut-string. html'

echo ${var:7}

The 7 means starting from the 8th character on the left

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

3. Which one from the right? The start of characters and the number of characters, usage: 0-start:len, for example:

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

echo ${str:0- 15:10}

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

The result is: cut-string

4. Starting from the character on the right to the end, usage: 0-start, for example:

str='http://www.your domain name.com/cut- string.html'

echo ${str:0-4}

where 0-6 means starting from 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)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn