Summarization of Shell intercepting string method in Linux

Source: Internet
Author: User
Tags first string

There are many ways to intercept strings in the shell,

${expression} A total of 9 ways to use.
${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 character 3 to the left, using 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 results obtained are http://www. your domain name. com, that is, delete from the right start to the first "/" and all the characters on the right 4, using the percent sign 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, delete from the right start to the last "/" and all the characters on 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 is: Http:2, starting at the beginning of the left and ending with the following: 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.HTML3, starting with the first few characters on the right, and the number of characters, use: 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 is: Cut-string3, starting from the right first few characters to the end, usage: 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 is: HTML Note: (the first character on the left is 0, and the first character on the right is represented by 0-1).

Other reference content on the Internet


The first 8 bits of a character variable are intercepted by the Linux shell, with the following methods:
1.expr substr "$a" 1 8
2.echo $a |awk ' {print substr (, 1,8)} '
3.echo $a |cut-c1-8
4.echo $
5.expr $a: ' (. \). * '
6.echo $a |dd Bs=1 count=8 2>/dev/null

Second, intercept by the specified string
1, the first method:
${varible##*string} to intercept the string after the last string from left to right
${varible#*string} to intercept the string after the first string from left to right
${varible%%string*} to intercept the string after the last string from right to left
${varible%string*} to intercept the string after the first string from right to left
"*" is just a wildcard character that you can not

Example:
$ myvar=foodforthought.jpg
$ echo ${myvar##*fo}
Rthought.jpg
$ echo ${myvar#*fo}
Odforthought.jpg

2, the second method: ${varible:n1:n2}: Intercept the variable varible from N1 to N2 between the string.

You can select a specific substring by using a variable extension of another form, depending on the specific character offset and length. Try entering the following line in bash:
$ exclaim=cowabunga
$ echo ${exclaim:0:3}
Cow
$ echo ${exclaim:3:7}
Abunga

This form of string truncation is simple, with a colon separated to specify the starting character and substring length.

Divided according to the specified requirements:
Like getting the suffix name
Ls-al | Cut-d "."-f2

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.