The Cut command extracts text columns from a text file or text stream.
Cut grammar
[[email protected] ~]# cut-d ' delimited characters '-f fields <== used to have a specific delimiter character
[[email protected] ~]# cut-c character range <== used to arrange neat information
Options and Parameters:
-D: followed by delimited characters. Used with-F;
-F: Divides a piece of information into several paragraphs according to the delimiter character of-D, and uses-F to remove the meaning of the paragraph;
-C: Remove the fixed character range in units of character (characters);
The PATH variable is as follows
[Email protected] ~]# echo $PATH
/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/x11r6/bin:/usr/games
# 1 | 2 | 3 | 4 | 5 | 6 | 7
To take the path variable out, I'm going to find a fifth path.
#echo $PATH | Cut-d ': '-F 5
/usr/local/bin
Take the path variable out and I'll find the third and fifth paths.
#echo $PATH | Cut-d ': '-f 3,5
/sbin:/usr/local/bin
Take the path variable out and I'll find the third to last path.
echo $PATH | Cut-d ': '-f 3-
/sbin:/usr/sbin:/usr/local/bin:/usr/x11r6/bin:/usr/games
Take the path variable out and I'll find the first to third path.
#echo $PATH | Cut-d ': '-f 1-3
/bin:/usr/bin:/sbin:
Take the path variable out, I want to find the first to third, there is a fifth path.
#echo $PATH | Cut-d ': '-f 1-3,5
/bin:/usr/bin:/sbin:/usr/local/bin
Practical Example: Display only/etc/passwd users and shells
#cat/etc/passwd | Cut-d ': '-f 1,7
Root:/bin/bash
Daemon:/bin/sh
Bin:/bin/sh
Intercepts a specified number of characters
$ A= ' echo Root:x:0:0:root:/root:/bin/bash | Cut-c 2-5 '//intercept 2nd to 5th characters
$ echo $a
Oot:
$ A= ' echo Root:x:0:0:root:/root:/bin/bash | Cut-c-2 '//intercept first two characters
$ echo $a
Ro
$ A= ' echo Root:x:0:0:root:/root:/bin/bash | Cut-c 2-'//intercept the 2nd one after
$ echo $a
Oot:x:0:0:root:/root:/bin/bash
Specify the file, and the last parameter is the filename
$ > Cat Cut_test.txt
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Adm:x:3:4:adm:/var/adm:/sbin/nologin
$ > cut-d:-F 1,6/cut_test.txt
Root:/root
Bin:/bin
Daemon:/sbin
Adm:/var/adm
Or with cut-d:-F 1,6 cut_test.txt and cat Cut_test.txt | Cut-d:-F 1,6 All can
Shell-cut usage