thelinuxvault blog

Mastering the `cut` Command in Linux

In the world of Linux command - line utilities, the cut command stands out as a powerful tool for text processing. It allows you to extract specific portions of text from a file or standard input, making it invaluable for tasks such as data cleaning, report generation, and more. Whether you're a novice Linux user looking to learn basic text manipulation or an experienced system administrator dealing with large datasets, the cut command can significantly streamline your workflow. This blog post will provide a comprehensive guide to using the cut command effectively.

2026-06

Table of Contents#

  1. Basic Syntax of the cut Command
  2. Cutting by Character Positions
  3. Cutting by Field Positions
  4. Specifying the Delimiter
  5. Combining Multiple Options
  6. Using cut with Standard Input
  7. Common Practices and Best Practices
  8. Conclusion
  9. References

1. Basic Syntax of the cut Command#

The basic syntax of the cut command is as follows:

cut [option] [file]
  • [option]: This specifies how you want to cut the text. There are various options such as -c for cutting by characters, -f for cutting by fields, etc.
  • [file]: This is the name of the file from which you want to extract the text. If you don't specify a file, cut will read from standard input.

2. Cutting by Character Positions#

The -c option allows you to cut text based on character positions. You can specify single characters, a range of characters, or a combination of both.

Example 1: Extracting a Single Character#

Suppose you have a file named example.txt with the following content:

Hello World

To extract the 5th character from each line, you can use the following command:

cut -c 5 example.txt

The output will be:

o

Example 2: Extracting a Range of Characters#

To extract characters from position 2 to 6, use the following command:

cut -c 2-6 example.txt

The output will be:

ello 

Example 3: Extracting Multiple Non - contiguous Characters#

You can extract multiple non - contiguous characters by listing them separated by commas. For example, to extract the 1st, 3rd, and 5th characters:

cut -c 1,3,5 example.txt

The output will be:

Hlo

3. Cutting by Field Positions#

The -f option is used to cut text based on fields. A field is a portion of a line separated by a delimiter (by default, the delimiter is a tab character).

Example 1: Extracting a Single Field#

Suppose you have a file named data.txt with the following tab - separated content:

Name    Age    City
John    25     New York
Jane    30     Los Angeles

To extract the 2nd field (Age) from each line, use the following command:

cut -f 2 data.txt

The output will be:

Age
25
30

Example 2: Extracting a Range of Fields#

To extract fields 2 to 3, use the following command:

cut -f 2-3 data.txt

The output will be:

Age    City
25     New York
30     Los Angeles

4. Specifying the Delimiter#

By default, cut uses the tab character as the delimiter when cutting by fields. However, you can specify a different delimiter using the -d option.

Example#

Suppose you have a file named csv_data.csv with comma - separated values:

Name,Age,City
John,25,New York
Jane,30,Los Angeles

To extract the 2nd field (Age) using a comma as the delimiter, use the following command:

cut -d ',' -f 2 csv_data.csv

The output will be:

Age
25
30

5. Combining Multiple Options#

You can combine multiple options to achieve more complex text extraction.

Example#

You can combine multiple options to achieve more complex text extraction. For instance, you can extract specific fields using -f and then further process them with other commands:

cut -d ',' -f 1 csv_data.csv

This extracts the first field (Name) from the CSV file.

6. Using cut with Standard Input#

cut can also read input from standard input. This is useful when you want to pipe the output of one command into cut.

Example#

The ls -l command lists files with detailed information. Suppose you want to extract the 9th field (file name) from the output of ls -l. You can use the following command:

ls -l | tr -s ' ' | cut -d ' ' -f 9

This command pipes the output of ls -l into tr -s ' ' to collapse multiple spaces, then into cut which extracts the 9th field (file name) using a space as the delimiter.

7. Common Practices and Best Practices#

Common Practices#

  • Data Cleaning: Use cut to remove unwanted parts of a text file, such as leading or trailing whitespace or specific columns in a dataset.
  • Report Generation: Extract relevant information from log files or other large text - based reports to generate concise summaries.

Best Practices#

  • Error Handling: Check if the file you're trying to process exists before running the cut command. You can use conditional statements in shell scripts to handle errors gracefully.
  • Efficiency: When working with large files, consider using more specialized tools if cut becomes a bottleneck. However, for most common text processing tasks, cut is fast and efficient.

8. Conclusion#

The cut command in Linux is a versatile and powerful tool for text processing. Whether you're working with small text files or large datasets, it can help you extract the exact information you need. By understanding its various options and how to combine them, you can perform complex text extraction tasks with ease. Remember to follow common and best practices to make your workflows more efficient and error - free.

9. References#