Shell greedy match

Source: Internet
Author: User

Sometimes, you can extract only a part of a text segment by using shell's greedy match. % is a match from the right to the left, and # Is a match from the left to the right, as shown in the following example:

[Root @ WWW ~] #Qq = www.qq.com

[Root @ WWW ~] #Echo $ {QQ #*.}// Exclude the first
QQ.com
[Root @ WWW ~] #Echo $ {QQ ##*.}// Exclude the first two
Com
[Root @ WWW ~] #Echo $ {QQ % .*}// Exclude the last one
Www. qq
[Root @ WWW ~] #Echo $ {QQ % .*}// Exclude the last two
WWW
[Root @ WWW ~] #

The above is a simple example. Let's take a look at the usage in a specific script.

#Vim test. Sh
#Chmod + X test. Sh
#CAT test. Sh

 
#! /Bin/bashnum = 1for I in *. tar * .tar.gz donew = new _ $ num. $ {I #*.} MV $ I $ new 2>/dev/nullif [$? -EQ 0]; then Echo "remove $ I to $ new" Let num ++ fi done

#Touch test2.161..52.16.tar test2.161..52.16.tar.gz
#Ls
Test1.tar test2.tar test3.tar test4.tar test5.tar test. Sh
Test1.tar.gz test2.tar.gz test3.tar.gz test4.tar.gz test5.tar.gz
#./Test. Sh
Remove test1.tar to new_1.tar
Remove test2.tar to new_2.tar
Remove test3.tar to new_3.tar
Remove test4.tar to new_4.tar
Remove test5.tar to new_5.tar
Remove test1.tar.gz to new_6.tar.gz
Remove test2.tar.gz to new_7.tar.gz
Remove test3.tar.gz to new_8.tar.gz
Remove test4.tar.gz to new_9.tar.gz
Remove test5.tar.gz to new_10.tar.gz
#

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.