cut Command in Linux



cut is a Linux command that is used mainly for text manipulation. It allows users to extract specific portions of text from file or data streams. You can use cut command to isolate columns from structured data files, like CSV or TSV files. Besides that, you can also split lines based on delimiters, such as commas and colons, or extract characters of bytes.

Table of Contents

Here is a comprehensive guide to the options available with the cut command −

cut Command Installation in Linux

The cut command comes under coreutils package utility, which is the main system package and pre installed in Linux systems. In case you are having trouble working with the cut command in Linux, you can reinstall it again from the default package manager.

If you are using a Debian-based Linux distribution like Ubuntu or Debian, the cut command can be re-installed using the below-given command −

sudo apt install --reinstall coreutils

If you are using a Linux distribution that doesn’t use APT as the package manager (such as Fedora, CentOS, or Arch Linux), you can install the cut command using the appropriate package manager for your system.

1. Fedora − To install cut on Fedora, use the following command −

sudo dnf install coreutils

2. CentOS − For CentOS, you can install it with the below-given command −

sudo yum install coreutils

3. Arch Linux − On Arch Linux, you can install it with the following command −

sudo pacman -S coreutils

Once installed, you can use the cut command to manipulate text files on Linux systems.

Syntax for cut Command in Linux

The basic syntax to use the cut command in Linux is provided below −

cut [OPTIONS] [FILE]

In the above syntax, you can replace [OPTIONS] with the specific flags you want to use while [FILE] with the path to the file you want to process.

Different Options Available for cut Command

With cut command, there are different options you can use, these are discussed in the table provided below −

Options Description
-b, --bytes=LIST Select specific bytes from each line of input; you can specify individual byte numbers, ranges, or sets.
-c, --characters=LIST Let you choose specific characters (based on their position) from each line.
-d, --delimiter=DELM Specify a custom delimiter to use instead of the default TAB delimiter.
-f, --fields=LIST Select specific fields (columns) from each line; you can specify field numbers, ranges, or sets.
-n, --complement Display all characters, bytes or fields excluding the selected ones.
-s, --only-delimited Print lines that contain delimiters (suppresses lines without delimiters).
--output-delimiter=STRING Specify a different output delimiter (default is the input delimiter).
-z, --zero-terminated Treats input lines as zero-terminated (instead of newline-terminated).
--help Displays help information about the cut command.
--version Displays the version information for the cut command.

Examples of cut Command in Linux

Let’s explore some examples of cut command in Linux −

  • Extract Specific Bytes
  • Cut by Character
  • Cut by Field
  • Output Delimiter

Extract Specific Bytes

One of the basic functions of cut command includes extracting specific bytes from each line. You can do this by using -b option with the command. For example, to extract bytes 1, 2, and 3 from the file named file.txt, use the following command −

cut -b 1,2,3 file.txt

The above command will extract the first three bytes, which correspond to the characters "H," "e," and "l.", resulting in generating the following output −

Extract Specific Bytes

Cut by Character

You can also use -c option with the cut command to select specific characters based on their position from each line. Consider the same text file, but now we will apply the text manipulation operation using the following command −

cut -c 1,2,3 file.txt

The command provided above will extract the first three characters, which correspond to the characters "H," "e," and "l.", as shown in the output below −

Cut by Character

Cut by Field

The cut command can also be used to select specific fields (columns) from each line by using the -d and -f options. For this example, we will use a CSV file named file.csv instead of a text file.

If you want to extract the second field from the CSV file, you can use the following command −

cut -d ',' -f 2 file.csv
Cut by Field

The above command will extract the second field, which contains Gender of persons. The -d ',' specifies that the delimiter between fields is a comma while the -f 2 selects the second field.

Output Delimiter

You can also specify the --output-delimiter=STRING with the cut command for specifying a different delimiter for the output. For example, to use a semicolon (;) as the output delimiter and extract the first and second fields, you can use the following command −

cut -d ',' -f 1,2 --output-delimiter=';' file.csv
Output Delimiter

Here, the -d ',' specifies that the delimiter between fields in the input file is a comma. The -f 1,2 selects the first and second fields, while the --output-delimiter=';' sets the output delimiter to a semicolon.

That’s how you can use the cut command on your Linux system.

Conclusion

cut is a powerful Linux command that is used for extracting specific portions of text from files or any other input streams. Whether you need to work with bytes, characters, or fields, the cut command is an efficient way to manipulate and process data. This guide has explored the cut command’s syntax, different options and examples to showcase the importance of the command. You can follow these examples to become proficient with the command and effectively utilize its capabilities.

Advertisements