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