The implementation of string interception under Linux can be broadly divided into two types, using commands to intercept, and using tools for interception. The specific interception methods are as follows:
A, #截取, can be implemented to delete the left character, retain the right character
Delete </> and all previous characters from the first </> on the left
Str=http://www.baidu.com/index.htmlecho ${str#*/} # Right First/output:/www.baidu.com/index.html
B, # #截取, you can delete the left character, leave the right character
Delete </> and all previous characters from the far right </> start
Str=http://www.baidu.com/index.htmlecho ${str##*/} # rightest/output: index.html
C,% intercept, can be implemented to delete the right character, leaving the left character
Delete </> and all characters to the right from the first </> on the right
Str=http://www.baidu.com/index.htmlecho ${str%/*} # Left Firft/output: http://www.baidu.com
d, Percent intercept, you can delete the right character, leave the left character
Delete </> and all characters to the right from the first </> on the left
Str=http://www.baidu.com/index.htmlecho ${str%%/*} # leftest/output: http:
e, interval interception
Intercept the No. 0 to 6th character
Str=http://www.baidu.com/index.htmlecho ${str:0:6} output: http:/
F, forward interval intercept to end
Intercept starts from 7th character to end
Str=http://www.baidu.com/index.htmlecho ${str:7} output: www.baidu.com/index.html
g, reverse interval intercept
Intercept the top 5 of the No. 0 to 7th characters
Str=http://www.baidu.com/index.htmlecho ${str:0-7:5} output: ex.ht
h, Reverse intercept, to the end
Intercept from the last 10th character to the end of the string
Str=http://www.baidu.com/index.htmlecho ${str:0-10} output: index.html
I, using the Cut command to implement string interception
Cut [Options] Options:-B----> byte-c----> character-f----> domain created a file with the following contents: [ [email protected] 3_26]$ cat FILEABCDEFG1234567POIUYTR
Examples of using cut interception are as follows:
[email protected] 3_26]$ Cat File | Cut-b 3c3i[[email protected] 3_26]$ cat File | Cut-b -3abc123poi[[email protected] 3_26]$ cat File | Cut-b 3-cdefg34567iuytr[[email protected] 3_26]$ cat File | Cut-b 3-5cde345iuy[[email protected] 3_26]$ cat File | Cut-b 3-5,7cdeg3457iuyr
For single-byte,-B and-C seem to work the same, but if the text appears in the case of Chinese,-C is able to correctly output a Chinese character, but using the-B option output is garbled, because a Chinese is two bytes. To solve this problem, the-B option is usually used with the-n option, and-n is used to tell the cut to intercept N-byte characters.
The following explains the role of domain <-f>. All the user information is saved in the/etc/passwd file, and you can see that each long string is separated by: We can assume that the data in the file is delimited by: separate domains. Specifies that the domain delimiter uses the-D option, followed by-F with a number format that is exactly the same as-B. "Cut's field delimiter can only be one character"
[Email protected] 3_26]$ CAT/ETC/PASSWD | Head-n 5root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/ Nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin[[email protected] 3_26]$ CAT/ etc/passwd | Head-n 5 | Cut-d:-F 1ROOTBINDAEMONADMLP
Needless to say, look at the following example
[[Email protected] 3_26]$ PS PID TTY time CMD 5630 pts/2 00:00:00 bash 5739 pts/2 00:00:00 ps[[email prote CTED] 3_26]$ PS | Cut-b 4i344
Obviously only three lines, but cut out four lines of content, because each command is actually a parent bash created alone a process, cut is no exception (built-in command).
-----Muhuizz Finishing
This article is from the "Twilight Back" blog, make sure to keep this source http://muhuizz.blog.51cto.com/11321490/1910482
Summary of implementation of string interception method under Linux