A method of intercepting a string that the cut cannot do in the shell

Source: Internet
Author: User

I've been writing a script for the last two days and learned three days, then wrote two days of time, feeling OK, also not very difficult, is some things are not ripe, feel the shell inside some processing and C voice difference is quite large, I think the logic can be achieved in the writing in the inside is not, can not be as c as you want to gain, Probably the most mature C logic and grammar bar. Here is a string processing on the Internet, because I wrote the script to read the file format, last night for a good while, non-stop tune, and finally a file of each row read out, and then divided into different groups, now have to put in each group of what I need to get out, That is, the string segmentation. I saw a cut command to split the string a few days ago, but now I find that it doesn't really meet my needs, like [SDVersion.dll] ver=5.0.0.5104 string in this format, cut seems too weak, and now there are other ways to handle these strings.


There are a number of ways to intercept strings in the shell, ${expression} There are 9 ways to use them.
${parameter:-word}
${parameter:=word}
${parameter:?word}
${parameter:+word}
4 of the above can be used to replace the default values.
${#parameter}
The above can get the length of the string.
${parameter%word} to intercept word from the back minimum
${parameter%%word} maximum intercept word from behind
${parameter#word} to intercept word from the front
${parameter# #word} maximum intercept word from the front
The top 4 is the way to intercept strings.
With four usages, you don't have to use the cut command to intercept the string.
The first one can be divided into four kinds of situations, described below.


1, using the # operator. The purpose is to start from the left. The first occurrence of a substring is the left character, which retains the right character. The usage is #*substr, for example:
Str= ' http://www. your domain name. com/cut-string.html '
Echo ${str#*//}
The results obtained are www. your domain name, com/cut-string.html, deletes all characters from the left to the first "//" and its left 2, using the # # operator. The purpose is to start from the left to delete the last occurrence of the substring, which is the left character, leaving the right character. The usage is ##*substr, for example:
Str= ' http://www. your domain name. com/cut-string.html '
Echo ${str##*/}
The resulting result is cut-string.html, which deletes the last occurrence of "/" and all characters to the left
3, use the% operator. The purpose is to start from the right. The first occurrence of the substring is the right character, leaving the left character. The usage is%substr*, for example:
Str= ' http://www. your domain name. com/cut-string.html '
Echo ${str%/*}
The result is http://www. your domain name. com, which deletes all characters from the right start to the first "/" and to the right
4, the use of the "%" operator. The purpose is to start from the right to delete the last occurrence of the substring, which is the right character, leaving the left character. The usage is%%substr*, for example:
Str= ' http://www. your domain name. com/cut-string.html '
Echo ${str%%/*}
The result is http://www. your domain name. com, that is, remove all characters from the right start to the last "/" and to the right


The second type is also divided into four kinds, which are described as follows:
1, from the left the first few characters and the number of characters, the use of: Start:len, for example:
Str= ' http://www. your domain name. com/cut-string.html '
Echo ${var:0:5}
0 represents the beginning of the first character on the left, and 5 represents the total number of characters.
The result: http:
2, starting from the left of the first few characters until the end, the use of: start, for example:
Str= ' http://www. your domain name. com/cut-string.html '
Echo ${var:7}
7 of which means the 8th character on the left begins
The result is: www. your domain name. com/cut-string.html
3, from the right of the first few characters and the number of characters, usage: 0-start:len, for example:
Str= ' http://www. your domain name. com/cut-string.html '
Echo ${str:0-15:10}
0-6 of these represent the start of the 6th character on the right, and 10 for the number of characters.
The result: cut-string
3, starting from the right of the first few characters to the end, use: 0-start, for example:
Str= ' http://www. your domain name. com/cut-string.html '
Echo ${str:0-4}
0-6 of these represent the start of the 6th character on the right, and 10 for the number of characters.
The result: HTML

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

The author of the string processing above is: Xwdreamer, although there are a lot of these things on the internet, but still want to respect the author of Ah.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.