Shell The technique of text slicing in
In the shell there is also a string slice this thing exists, has been used in Python slices, the following record the use of slices in the shell.
(1) , replacing some of the text in the variable contents
$var = "This was a line of text"
$ echo${var/line/replaced}
This is areplaced of text "
Line be replaced by replaced .
(2) , specifying the starting position and length of the string to generate the substring
The syntax is as follows:
${variable_name:start_position:length}
Print Section 5 after a character:
$string =abcdefghijklmnopqrstuvwxyz
$ echo${string:4}
Efghijklmnopqrstuvwxyz
from the first 5 characters start, print 8 characters:
$ echo${string:4:8}
Efghijkl
the index of the starting character is from 0 start counting. We can also count from the back forward and write the last character index as -1. However, if you use negative numbers as index values, you must enclose the negative numbers in parentheses. ( -1) is the index of the last character.
Echo${string: (-1)}
Z
$ echo${string: (-2): 2}
Yz
This article is from the "operation and maintenance of micro-letter" blog, please be sure to keep this source http://weixiaoxin.blog.51cto.com/13270051/1963717
Linux Shell Programming---Text slicing technology in the shell