Master the Linux ‘cut’ Command: A Comprehensive Guide

Peter Hou
3 min readMay 3, 2023

--

Master the Linux ‘cut’ Command: A Comprehensive Guide

This article provides an in-depth guide for Linux newbies to learn about the cut command, a powerful text manipulation tool. It covers the command's history, usage, and common parameters, as well as other supported options. The article also highlights various use cases, tricky skills, and important points to be aware of when using the cut command. By mastering this tool, users can efficiently process text data, extract desired fields, and perform complex text operations, especially when handling log files or CSV data.

Instructions

This article provides a detailed introduction to the Linux ‘cut’ command, including its history, usage, parameters, and common use cases.

History

The ‘cut’ command has been part of Unix-like operating systems since the early days of Unix. It is a text-processing utility that has proven to be a useful tool in many situations.

When and why to use it

The ‘cut’ command is used to remove sections from each line of a file or standard input. It is particularly useful for extracting specific fields from text data, such as log files or columnar data, and can be combined with other commands to process text data efficiently.

How to use it

To use the ‘cut’ command, provide the input data, specify the delimiter (if any), and the fields or characters to be extracted.

cut [OPTIONS] [FILE]

The commonly used parameters

  • -d Specify the delimiter used to separate fields in the input data.
cut -d ',' -f 1 input.csv
  • -f Select the fields to be extracted, specified as a comma-separated list of field numbers.
cut -d ':' -f 1,5 /etc/passwd
  • -c Select the characters to be extracted, specified as a comma-separated list of character positions.
cut -c 1-3,5 input.txt
  • --complement Invert the selection, outputting the fields or characters that were not selected.
cut -d ':' -f 1,5 --complement /etc/passwd
  • -s Suppress lines with no field delimiter characters. Only meaningful when using f.
cut -d ':' -f 1 -s input.txt

Other supported parameters

  • -n Do not split multi-byte characters (useful for processing UTF-8 encoded text).
  • --output-delimiter Specify the string used to separate fields in the output.

Most common use cases

  • Extracting specific fields from CSV or TSV files.
  • Parsing log files to extract relevant data.
  • Removing certain columns from text data.
  • Filtering data based on specific fields.
cut -d ':' -f 1 /etc/passwd | sort | uniq

The tricky skills

  • Combining ‘cut’ with other commands, such as ‘grep’, ‘sort’, and ‘uniq’, to perform complex text processing tasks.
  • Using ‘cut’ to remove multiple ranges of characters or fields from input data.
  • Chaining multiple ‘cut’ commands to extract nested fields.
cut -d ':' -f 5 /etc/passwd | cut -d ',' -f 1

What needs to be noted

  • Be aware of the delimiter being used, especially when processing CSV files that may contain quoted fields with embedded delimiters.
  • Ensure proper handling of multi-byte characters when using the -c option.

Conclusion

The ‘cut’ command is a powerful text-processing tool in Linux. Its flexibility and simplicity make it an essential utility for extracting specific fields or characters from text data. By mastering the ‘cut’ command and combining it with other Linux commands, you can perform a wide range of text manipulation tasks efficiently.

--

--

Peter Hou

I am a Senior Software Engineer and tech lead in a top tech company.