Linux cut command: output selected bytes, characters or fields from each line of the file
Linux cut command Function Description
Use the cut command to output selected bytes, characters, or fields (fields) from each line of a file.
Only one of the -b, -c or -f options can be used.
Each list is made specifically for a category, or different categories can be separated by commas to be displayed simultaneously.
The input order will be used as the read order, and each can be entered only once.
Linux cut command Syntax
cut [option] [ file ]
The meaning of each option in the command is shown in the table:
Option | Description |
---|---|
-b <List> |
Select only those bytes specified |
-c <List> |
Select only those characters specified |
-d <List> |
Use specified delimiters instead of tabs as region delimiters |
-f <List> |
Specify the list of fields in the file that are envisaged to be delimited (by default, tabs) |
-n | Unseparate multi-byte characters. Use only with the -b option. If the last byte of a character falls within the range indicated by the -b option’s list argument, the character will be written out; otherwise the character will be excluded |
-s | Do not print lines that do not contain a delimiter |
When using the -b, -c, and -f options, each parameter format represents the following ranges:
- N: The Nth byte, character or field counting from the first.
- N-: All characters, bytes or fields from the beginning of the Nth to the end of the line in which it is located.
- N-M: All characters, bytes or fields from the Nth up to and including the Mth.
- -M: All characters, bytes or fields from the first one up to and including the Mth one.
Linux cut command Demo
cut outputs selected bytes
cut outputs the selected character
Display the user login name and username full name fields in the file /etc/passwd, which are the first and fifth fields, separated by a colon
[root@rhel ~]# cut -f 1,5-d: /etc/passwd
root:root
bin:bin
daemon:daemon
adm:adm
lp:lp
sync:sync
shutdown:shutdown
halt:halt
mail:mail
uucp:uucp
operator:operator
games:games
gopher:gopher
ftp:FTP User
nobody:Nobody
dbus:System message bus
usbmuxd:usbmuxd user
vcsa:virtual console memory owner
rpc:Rpcbind Daemon
rtkit:RealtimeKit
avahi-autoipd:Avahi IPv4LL Stack
abrt:
rpcuser:RPC Service User
nfsnobody:Anonymous NFS User
haldaemon:HAL daemon
gdm:
ntp:
apache:Apache
saslauth: ''Saslauthd user''
postfix:
pulse:PulseAudio System Daemon
sshd:Privilege-separated SSH
tcpdump: