Shell programming--slicing file names

Source: Internet
Author: User

Citation: Sometimes we meet this demand, we need to xxx.yyy the name xxx and extension yyy from the file name. These can be done using the "%", "percent", "#" and "# #" operators.

A. "%" operator:

1. Example:

Example 1

Example 2

2. Explanation:

Example the value of the 1:file variable is "sample.jpg". File%.* means that the match is started from the right side of the file variable (sample.jpg in the example) until the first match is encountered. * The string (in the example,. jpg), and then delete it. So the value of the variable name is sample.

Example 2: If the value of the file variable is "Sample.jpg.txt", then file%.* will only delete. txt, and name will be sample.jpg. That is, the% operator is non-greedy. When the first match is encountered, it stops.

3. Note:

    • % is not a regular expression, * matches any long character,? Matches one character. such as 1?2 can match 132 and 1a2;a* can match any string starting with a.
    • The match is from the last character, right-to-left. File%.*m (the variable file value is Sample.jpg) does not erase any characters. Because the last character of the variable is G, there will never be a match. *m string.

Two. "Percent" operator:

1. Example:

2. Explanation:

The difference between "percent" and "%" is that '% ' is greedy, meaning it matches the longest string that meets the criteria. So in the example, file%%.* will start matching from the right, the longest match is. Jpg.txt, and then delete it. So the value of the variable name is sample.

Three. "#" operator

1. Example:

Example 1

Example 2

2. Explanation:

Example 1:file#*. is to perform a non-greedy match from right to left. Find the first match *. String (in the example "sample.") ), and then delete it. So the variable ext is "JPG".

Example 2: It shows that the match of # is non-greedy, and the first match is stopped.

Four. "# #" operator

1. Example:

2. Explanation:

Similar to "percent", "# #" is the greedy pattern of "#", which matches the longest string matching the criteria from right to left. So the example will delete "SAMPLE.JGP." In "Sample.jpg.txt".

Shell programming--slicing file names

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.