9 Kinds of interception of strings in shell, take var=http://www.google.com/test/.html as an example
(1) Method one: # Intercept, delete the left character, retain the right character.
Echo ${var#*//}
var is the variable name, and the # is the operator, *//means to delete the first//number and all characters from the left, starting from the left.
(*: Match 0 or more of any characters)
Delete http://
The result is: www.google.com/test.html
(2) method two: # # Intercept, delete the left character, retain the right character.
Echo ${var##*/}
##*/indicates that the last (rightmost) one/number and all characters on the left are deleted from the left.
That is, delete http://www.google.com/
The result is test.html.
(3) method three:% intercept, delete the right character, leave the left character
Echo ${var%/*}
%/* indicates that the first/second and right characters are deleted from the right.
The result is: http://www.google.com
(4) method Four: The percent number intercept, delete the right character, leave the left character
Echo ${var%%/*}
%%/* indicates that the last (leftmost) one/number and right character are deleted from the right.
The result is: http:
(5) method five: Starting from the first few characters on the left, and the number of characters
Echo ${var:0:5}
0 represents the first character on the left and 5 indicates the total number of characters.
The result is: http:
(6) method six: Starting from the first few characters on the left, until the end.
Echo ${var:7}
7 means that the 8th character on the left begins, until the end.
The result is: www.google.com/test.html
(7) method Seven: Start with the first character on the right and the number of characters
Echo ${var:0-7:3}
0-7 means that the seventh character starts at the right, and 3 indicates the number of characters.
The result is: test
(8) method eight: Starting from the first few characters on the right, until the end.
Echo ${var:0-7}
The expression starts at the seventh character on the right and continues to the end.
The result is: test.html
(9) Method IX: Split according to the specified requirements.
For example, get the suffix name
Ls-al | Cut-d "."-f2
The Cut command can split a line of characters by a specified delimiter,-d specifies the delimiter,-f specifies which character to output
such as: echo "a/b/c" |cut-d '/'-F 1.
The result of the execution is a.
Execution process: First press/segment, the result is: The first field is a, the 2nd field is B, and the 3rd field is C,-f is the number of fields.
Note: (The first character on the left is denoted by 0, and the first character on the right is denoted by 0-1)
Related commands
: command: Intercept by number.
# command: Get the right character, starting with the first one from the left.
# # command: Gets the right character, starting with the last one from the left. (% similar)
% command: Gets the left character starting from the right with the first
Percent command: Gets the left character starting from the right with the last
This article is from the "sunshine225" blog, make sure to keep this source http://10707460.blog.51cto.com/10697460/1790442
Interception of strings in the shell