Interception of strings in the shell

Source: Internet
Author: User

9 Kinds of interception of strings in shell, take var=http://www.google.com/test/.html as an example

(1) Method one: # Intercept, delete the left character, retain the right character.

Echo ${var#*//}

var is the variable name, and the # is the operator, *//means to delete the first//number and all characters from the left, starting from the left.

(*: Match 0 or more of any characters)

Delete http://

The result is: www.google.com/test.html

(2) method two: # # Intercept, delete the left character, retain the right character.

Echo ${var##*/}

##*/indicates that the last (rightmost) one/number and all characters on the left are deleted from the left.

That is, delete http://www.google.com/

The result is test.html.

(3) method three:% intercept, delete the right character, leave the left character

Echo ${var%/*}

%/* indicates that the first/second and right characters are deleted from the right.

The result is: http://www.google.com

(4) method Four: The percent number intercept, delete the right character, leave the left character

Echo ${var%%/*}

%%/* indicates that the last (leftmost) one/number and right character are deleted from the right.

The result is: http:

(5) method five: Starting from the first few characters on the left, and the number of characters

Echo ${var:0:5}

0 represents the first character on the left and 5 indicates the total number of characters.

The result is: http:

(6) method six: Starting from the first few characters on the left, until the end.

Echo ${var:7}

7 means that the 8th character on the left begins, until the end.

The result is: www.google.com/test.html

(7) method Seven: Start with the first character on the right and the number of characters

Echo ${var:0-7:3}

0-7 means that the seventh character starts at the right, and 3 indicates the number of characters.

The result is: test

(8) method eight: Starting from the first few characters on the right, until the end.

Echo ${var:0-7}

The expression starts at the seventh character on the right and continues to the end.

The result is: test.html

(9) Method IX: Split according to the specified requirements.

For example, get the suffix name

Ls-al | Cut-d "."-f2

The Cut command can split a line of characters by a specified delimiter,-d specifies the delimiter,-f specifies which character to output

such as: echo "a/b/c" |cut-d '/'-F 1.

The result of the execution is a.

Execution process: First press/segment, the result is: The first field is a, the 2nd field is B, and the 3rd field is C,-f is the number of fields.

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


Related commands

    1. : command: Intercept by number.

    2. # command: Get the right character, starting with the first one from the left.

    3. # # command: Gets the right character, starting with the last one from the left. (% similar)

    4. % command: Gets the left character starting from the right with the first

    5. Percent command: Gets the left character starting from the right with the last


This article is from the "sunshine225" blog, make sure to keep this source http://10707460.blog.51cto.com/10697460/1790442

Interception of strings in the shell

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.