Greedy and non-greedy matches in the shell

Source: Internet
Author: User

Give me a chestnut:

  

V=jfedu.shiyiwen.comecho ${v%.*}

A% is a non-greedy match, which matches the shortest result. % a non-greedy match from right to left, what does it match? Matches characters that have. *. Then of course it matches to. com and then executes the delete match to the character. The result is (the "." In this case equals the delimiter.) and "*" is a wildcard character)

Jfedu.shiyiwen

Echo ${v%%.*}

The greedy match is executed here, which matches the longest match. Shiyiwen.com result is

jfedu

Here is saying a # just as opposed to the% order, #是从左到右来进行匹配的. The usual: Give me a chestnut.

V=jfedu.shiyiwen.comecho ${v#*.} Shiyiwen.comecho ${v##*.} Com

There are many uses, the more common is very convenient to get the file suffix name and file name. Give me a chestnut.

01.#!/bin/bash  .  03.num=1  04.for i in *.tar *.tar.gz  05.do  06.new=new_$num.${i#*.}      07.MV $i $new 2>/dev/null  .  09.if [$?-eq 0];then  .    echo "Remove $i to $new"  .    Let num++  12.fi  .      14.done

In addition to some:

${#VALUE}: Calculates the number of characters in the VALUE string.

    • ${value%. *} or ${valuepercent. *}: Remove the VALUE string with the delimiter "." Matches the right character, preserving the left character.
    • ${value#*.} or ${value# #*.} : Remove the value string with the delimiter "." Matches the left character, preserving the right character.
    • ${value/old/new} or ${value//old/new} : Replaces the old substring in the value string with the new substring.

add:"*" indicates a wildcard character used to match strings that will be deleted. “.” Represents a delimiter in a string, which can be any one or more characters. "%" means right-to-left matching, "#" means matching from left to right, "\" for substitution, all of which are non-greedy matches, which match the shortest result matching the wildcard character. "percent", "# #" and "//" are similar to "%", "#" and "/", all of which are greedy matches, that is, match the longest result that matches the wildcard character.

    1. ${value: OFFSET} or ${value: offset:length}: Intercepts substrings from the left of the VALUE string.
    2. ${value: 0-offset} or ${value: 0-offset:length} : Intercepts the substring from the right start of the value string.

added: The first character on the left starts with "0" and the first character on the right begins with "0-1". Represents the offset at which a character is to be truncated and length. If there is no length variable, the offset is shifted from the beginning of the character to the end of the string.

    1. ${value:-word}: When the variable is undefined or the value is empty, the return value is the contents of WORD, otherwise the value of the variable is returned.
    2. ${value: =word}: When the variable is undefined or the value is empty, returns the value of Word and assigns word to value, otherwise returns the value of the variable.
    3. ${value: +word}: When a variable is assigned a value, its value is replaced with WORD, otherwise no substitution is made.
    4. ${value:? MESSAGE}: Normal replacement When the variable is assigned a value. Otherwise, message messages are sent to the standard error output (if this substitution appears in the shell program, the program terminates).

Add: Word can be a string, or it can be a variable. When you are a variable, you need to refer to the variable with "$".

For additional ${} use, please see another blog post.

Greedy and non-greedy matches 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.