1. Command function
cut cuts the selected part from each file and outputs it.
2. Grammar format
cut option file
Parameter Description
parameter |
Parameter Description |
-b (–bytes) |
byte |
-c (--characters) |
character |
-d |
Split the file by specifying the delimiter (the default delimiter is the tab key) |
-f (usually used in combination with -d) |
Select only the region to be output; also output lines without delimiters unless the -s option is specified. |
-n (with -b) |
Use in conjunction with -b, do not split on multibyte characters |
-s |
Do not output lines that do not contain delimiters (combined with -d) |
3. Example of use
Ready to work
[root@localhost chu]# cat passwd 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 joe:x:502:502::/home/joe:/bin/bash chu: x: 503: 503 :: / home / chu: / bin / bash abcdefhijklmnopqrstuvwxyz1234567890 a1b2c3d4e5f6h7i8j9k0lmnopqrstuvwxyz
Example 1 Output the 2nd to 5th characters of the file
[root@localhost chu]# cut -c 2-5 passwd waiting: in:x aemo dm:x oe: x hu: x bcde 1b2c
Example 2 Specify the colon as the separator, and output the first and third parts
[root@localhost chu]# cut -d ":" -f 1,3 passwd root:0 bin:1 daemon:2 adm:3 joe:502 chu:503 abcdefhijklmnopqrstuvwxyz1234567890 #There is no separator, the whole line is output a1b2c3d4e5f6h7i8j9k0lmnopqrstuvwxyz
Example 3 Specify the colon as the separator, output the first and third parts, and do not output the line without separator [parameter -s]
[root@localhost chu]# cut -d ":" -f 1,3 -s passwd root:0 bin:1 daemon:2 adm:3 joe:502 chu:503
Example 4 Specify the colon as the separator, output the first to third parts, and do not output the line without separator [parameter -s]
[root@localhost chu]# cut -d ":" -f 1-3 -s passwd root:x:0 bin:x:1 daemon:x:2 adm:x:3 joe:x:502 chu: x: 503