“Cut” Command in Linux


Introduction

The "cut" command in Linux is a powerful command line utility that allows you to extract specific sections of text from a file or data stream. Whether you're working with tab-delimited files, CSV files, or just want to extract a specific range of characters, the cut command is a versatile tool that can help you get the job done quickly and easily. In this article, we will discuss the basic usage of the cut command as well as some advanced options and examples of how it can be used. We'll also look at some real-world use cases for the cut command and how it can be used in conjunction with other Linux commands to create powerful word processing scripts.

Basic usage of cut command

The basic syntax of the cut command is as follows −

$ cut [options] [file]

The options specify the criteria for data extraction and file is the name of the file or data stream from which you want to extract data.

One of the more common uses of the cut command is to extract specific fields or columns from a file. By default, the break command uses the tab character as the delimiter between fields. For example, consider the following file, named "example.txt" −

John Doe, 35, Male
Jane Smith, 29, Female
Bob Johnson, 42, Male

To extract the first field (the name) from this file, use the following command −

$ cut -f1 -d, example.txt
John Doe
Jane Smith
Bob Johnson

Advanced options of cut command

The cut command has several advanced options that let you specify different delimiters, extract specific ranges of characters, and more. Some of the most used options are −

  • -f − This option is used to specify the field or column that you want to extract. You can specify multiple fields by separating them with a comma.

  • -d − This option is used to specify the delimiter that separates the fields in the file. By default, the delimiter is the tab character.

  • -b − This option is used to specify a range of bytes that you want to extract from the file.

  • -c − This option is used to specify a range of characters that you want to extract from the file.

Examples of cut command

Here are some examples of how the cut command can be used with some of these options −

  • Extracting specific fields

$ cut -f1,3 -d, example.txt
John Doe, Male
Jane Smith, Female
Bob Johnson, Male
  • Extracting specific bytes

$ cut -b 3-8 example.txt
hn Do
ane Sm
b John
  • Extraction of specific characters

$ cut -c 3-8 example.txt
hn Do
ane Sm
b John

Usage of cut command in the real life

The cut command can be used in many different ways to process text files and data streams. Here are some examples of how the cut command can be used in real-world scenarios −

Extracting specific columns from a CSV file −

$ cut -f1,3 -d, example.csv

Extracting specific fields from a log file −

$ cut -f1,4 -d" " logfile.txt

Extracting specific bytes from a binary file −

$ cut -b 500-1000 binaryfile.bin

Extracting specific characters from a string

$ echo "Hello World" | cut -c 3-8

Extracting specific fields from a tab-delimited file −

$ cut -f2-5 -d$'\t' tabfile.txt

Combine cut with other commands

The cut command can also be used in conjunction with other Linux commands to create powerful word processing scripts. For example, you can use the cut command to extract specific fields from a file, then pipe the output to the sort command to sort the fields alphabetically −

$ cut -f1 -d, example.txt | sort

You can also use the cut command to extract specific fields from a file, then pipe the output to the uniq command to remove duplicate entries −

$ cut -f1 -d, example.txt | sort | uniq

Conclusion

The cut command is a powerful command line utility on Linux that allows you to extract specific sections of text from a file or data stream. With the different options and examples discussed in this article, you should have a good understanding of how to use the cut command in your projects. Whether you're working with tab-delimited files, CSV files, or just want to extract a specific range of characters, the cut command is a versatile tool that can help you get the job done quickly and easily. Real world use cases and combination with other commands will give you an idea of ​​how this command can be used effectively in your daily routine.

Updated on: 13-Feb-2023

541 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements