2.12 Splitting file names by extension

Source: Internet
Author: User

---restore content starts---

Some scripts are processed by file names, and we may need to modify the file name, convert the file format (while retaining the file name and modify the extension), or extract part of the file name while preserving the extension. Some of the built-in features of the shell can be used to slice the file names according to different circumstances.

1) The% operator makes it easy to extract the name part from the "name. Extension" format, such as extracting names from simple.jpg

file_jpg = "Sample.jpg"

name=$ (file_jpg%.*)

echo File name is: $name

The output is as follows: File name is sample

The following section extracts the extension of the file name, which can be implemented with the # operator.

For example, extract the. jpg from the file name and store it in the variable file_jpg:

extension=$ (file_jpg#*.)

Echo Extension is:jpg

Output Result: Extension is:jpg

2) Working principle

For the first command to extract the file name,

The meaning of ${var%.*} is as follows:

A, remove the wildcard character at the right of the $var (the. * In the front row) that matches the string. Wildcard characters are matched from right to left.

b, assign value to VAR, var=sample.jpg. Then, the wildcard character will match from right to left to. jpg, so deleting the match result from the $var will give you the output sample.

% is a non-greedy operation that finds the shortest result of a matching wildcard from right to left. There is another operator, which is similar to%, but the behavior pattern is greedy, which means that it matches the longest string that meets the criteria. For example, there is such a file var=back.fun.book.txt

Use the% operator:

$ echo ${var%.*}

Output Result: Back.fun.book

The operator% is used. * Right-to-left non-greedy matching (. txt)

Use the percent-percent operator below:

$ echo ${var%%.*}

Output Result: Back

The operator is used with a percent. * Greedy match from right to left (. fun.book.txt)

In the second task, use the # operator to extract the extension from the file name, and its evaluation direction is left to right.

${var#*.}: The wildcard character that is located on the right side of the # from the $var (that is, the *.) string used in the preceding precedent matches the wildcard character from left to right, and it also has a greedy operator ##,# #从左向右进行贪婪匹配 and removes the matching result from the specified variable.

As follows

Var=back.fun.book.txt

Using the operator #:

$ echo ${var#*.}

Get output: Fun.book.txt

Operator # with *. Performs a non-greedy match (back) from left to right.

Use # #操作符:

$ echo ${var##*.}

Get output: txt

Note: Because the file name may contain more than one '. ' characters, so #更适合于从文件名中提取扩展名 compared to #,#.

A use case for extracting different parts of a domain name, assuming url= "www.google.com"

$echo ${url%.*} #移除. * Matches the right-most content

Www.google

$echo ${url%%.*} #将从右边开始一直匹配到最左边的 *. remove

Www

$echo ${url#*.} #移除 *. The leftmost content that matches

google.com

$echo ${url##*.} #将从左边开始一直匹配到最右边的 *. remove

Com

---restore content ends---

2.12 Splitting file names by extension

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.