Exploring Linux cut Command with Examples

The Linux cut command is a powerful utility that allows users to extract specific portions of text from a file or data stream. It is particularly useful for data manipulation and filtering. It can also be used to cut parts of a line by field, delimiter, byte position, and character. These features make it an important tool for text processing in shell scripts or command-line operations.

In this tutorial, we will explore the several aspects of cut command in Linux with possible examples.

How to Use Cut Command in Linux?

The cut command in Linux is a powerful tool used to extract particular portions of files. It operates by selecting sections of text based on bytes, characters, or delimited fields. It is commonly used for creating bash scripts and command-line utilities to manipulate text data and extract useful information. 

Basic Syntax

With its simple syntax and versatile options, the cut command provides an essential functionality for text processing tasks. The general syntax of the cut command is given below:

cut [OPTION] [FILE]

The [OPTION] specifies how the cut should be performed, and [FILE] is the name of the file to process. 

Common Option

The cut command can be used with various options to specify how the data should be cut. These options include:

  • -b or –bytes: This option allows you to select the bytes from each line of the file.
  • -c or –characters: With this option, you can specify the characters to be extracted.
  • -f or –field: This option is used to select fields or columns of data separated by a delimiter.
  • -d or –delimiter: This option specifies a custom delimiter, which by default is the tab character.
  • –complement: This option instructs cut to display all bytes, characters, or fields, except the selected ones.
  • -s or –only-delimited: This option skips lines that do not contain the delimiter.

Practical Examples of cut Command

Let’s explore some practical examples of how the cut command can be used:

1. Extracting Bytes by Byte Number

To extract specific bytes from a line, you can use the -b option. For example, to extract the first five bytes of each line from a file, use the below command:

cut -b 1-5 file.txt
extracting bytes by byte number using cut command

This extracts the first 5 bytes from each line of file.txt.

2. Extracting Characters by Character Number

If you want to extract certain characters, you can use the -c option. Let’s extract the first character of each line of the given file:

cut -c 1 file.txt
extract the first character of each line of the given file using cut command

This extracts the first characters H, H, U, and T from each line of file.txt.

Extracting a Range of Characters

To extract the range of characters, use the specific range such as 1 to 10 with the -c option:

cut -c 1-10 file.txt
Extracting a Range of Characters with cut command

This extracts characters 1 to 10 from each line of file.txt

Extracting Multiple Ranges of Characters

You can specify several ranges by separating them with a comma. For example, to keep characters from 1 to 10 and 20 to 30:

cut -c 1-10,20-30 file.txt
Extracting Multiple Ranges of Characters using cut command

It extracts multiple ranges of characters from the file.txt.

3. Extracting Fields with a Different Delimiter

The -f option is particularly useful when dealing with delimited data. To select the second field of each line using a comma as a delimiter, execute the below command:

cut -d ',' -f 2 file.txt
using cut command to extract fields with a different delimiter

This extracts the second field from each line of file.txt, assuming the delimiter is a comma.

Extracting Multiple Fields

Let’s extract the second and fourth fields from each line, assuming the delimiter is a comma:

cut -d "," -f 2,4 file.txt
extracting multiple fields with cut command

It extracts multiple fields from the file.txt.

Extracting a Range of Field

You can also extract a range of fields by specifying the file name. Let’s extract fields 2 to 4 from each line, skipping field 1:

cut -d " " -f 2-4 file.txt
extracting a range of field with cut command

This extracts fields from 2 to 4 of file.txt

4. Changing the Output Delimiter

To change the output delimiter, use the –output-delimiter option. For instance, change the output delimiter comma to a semicolon via the below command:

cut -d ',' -f 1,2 --output-delimiter=';' file.txt
changing output delimiter with cut command

The output shows that the output delimiter comma has been changed to a semicolon.

Suppressing Lines Without a Delimiter

To extract the first field only from lines that contain a comma, use the below command:

cut -d "," -s -f 1 file.txt
suppressing lines without a delimiter with cut command

This extracts the first field from the file.txt, but only if the line contains a comma.

5. Combining cut with Other Commands

The cut command can also be used with piped input to find a specific pattern. For example, to get the first field of all users on a system, use the getent with the cut command as below:

getent passwd | cut -d ':' -f1
piping cut command with other commands

This extracts the list of Linux users on Ubuntu from the first field of each line.

Using Complement Option

The –complement option displays everything except the specified fields, bytes, or characters. Let’s show everything except the first field having comma delimiter:

cut -d ',' --complement -f1 file.txt
using cut command with complement option

The output shows that the first line is skipped due to the delimiter comma.

By understanding these examples, you can effectively use the cut command to manipulate text data in your Linux environment. To explore more about the cut command, use the –help utility below:

cut --help
exploring help page of the cut command

Conclusion

In Linux, the cut command is straightforward and enables you to specify the exact data you wish to isolate, such as a range of characters or fields separated by a specific delimiter. Whether you are working with CSV files, log files, or any other text-based data, cut provides a simple and powerful way to achieve your text processing goals. In addition, the cut command is versatile and can be combined with other commands such as the grep command Linux or awk to perform complex text manipulation tasks.

The cut command is a powerful tool for extracting and manipulating text in files. You can practice using the cut command on Ultahost’s Free VPS hosting, giving you a great opportunity to experiment without any upfront costs. We offer a variety of VPS plans to choose from, so you can find one that meets your needs.

FAQ

What is the key use of the cut command in Linux?
How does the cut command work in Linux?
How can I extract specific fields from a CSV file using cut?
How do I select a range of characters using cut?
How to use cut Command in bash?
How do I change the output delimiter of the cut command?
Can I combine cut with other commands like grep or awk?

Related Post

How to Check Linux Version

Linux is an open-source operating system that offers a ...

How to Install Jira on Linux

Jira is a popular project management tool developed by ...

How to Install Deb Files / Packages on Ubuntu

The .deb packages are the standard format for installin...

Nohup Command in Linux

The Nohup command, which stands for "no hang up," is a ...

How to Install Tor on Kali Linux

Kali Linux the hacker's playground prioritizes security...

How to Check Running Processes in Linux?

In Linux, you can efficiently manage your system by che...

Leave a Comment