The cut command extracts the specified lines of text from a file. Common cut command options and their uses are given in the following table.
Option | Description |
---|---|
-c | Specify the number of the character to cut from each line. |
-d{delimiter} | Separate one field from another. |
-f{field numbers} | Specify the field numbers to cut on as separated by the delimiter. For example, -f2 indicates the field between the first and second instances of the delimiter. |
-s | Suppress a line if the delimiter is not found. |
Syntax
The syntax of the cut command is:
# cut [options] {file names}
The cut command is used to display only specific columns or characters from a text file or from other command outputs. For example, in the following command, we display the login names from the /etc/passwd file:
$ cut -d: -f1 /etc/passwd
If you encounter the below error while running the cut command:
cut: command not found
you may try installing the below package as per your choice of distribution:
Distribution | Command |
---|---|
OS X | brew install coreutils |
Debian | apt-get install coreutils |
Ubuntu | apt-get install coreutils |
Alpine | apk add coreutils |
Arch Linux | pacman -S coreutils |
Kali Linux | apt-get install coreutils |
CentOS | yum install coreutils |
Fedora | dnf install coreutils |
Raspbian | apt-get install coreutils |
cut Command Examples
1. Print a specific character/field range of each line:
# command | cut --characters|fields=1|1,10|1-10|1-|-10
2. Print a range of each line with a specific delimiter:
# command | cut --delimiter="," --characters=1
3. Print a range of each line of the specific file:
# cut --characters=1 path/to/file
4. The following command line displays the first and third fields from a colon-delimited file (extra lines stripped from output):
$ cut -d: -f1,3 /etc/passwd
5. The following command line display only the first four characters of every line in the /etc/passwd file:
$ cut -c 1-4 /etc/passwd