Skip to content

cut Command — Extracting Columns from Text

The cut command is used to extract specific columns, fields, or characters from text files. It is especially useful when working with structured data such as CSV files, log files, configuration files, and command output. Mastering cut helps Linux administrators and DevOps engineers quickly retrieve only the information they need.


Learning Path

Linux Mastery → Module 3: Text Processing → Lesson 3

Difficulty: Beginner → Intermediate

Reading Time: 30 Minutes

Course Progress

Course: Linux Mastery

Module: Text Processing

Lesson: 3 of 18


What You'll Learn

After completing this lesson, you'll be able to:

  • Understand the cut command
  • Extract characters from text
  • Extract fields using delimiters
  • Extract multiple fields
  • Work with CSV files
  • Process Linux configuration files
  • Combine cut with pipes

Prerequisites

Before starting this lesson, complete:

  • Module 1 – Linux Fundamentals
  • Module 2 – Linux Command Line Essentials
  • Module 3 Lessons 1–2

Why Learn cut?

Imagine you have the following file:

Alice,Engineering,India

Bob,HR,USA

Charlie,Finance,UK

David,Engineering,Germany

You only need:

Engineering

HR

Finance

Engineering

Instead of manually editing the file:

cut -d "," -f2 employees.csv

Done in seconds.


What is cut?

The cut command extracts selected portions of each line from a file.

It can extract:

  • Characters
  • Bytes
  • Fields

Syntax:

cut [OPTION] FILE

Sample File

Create a file.

cat > employees.csv

Contents:

Alice,Engineering,India

Bob,HR,USA

Charlie,Finance,UK

David,Engineering,Germany

Press:

Ctrl + D

Extract Fields

Fields are separated by delimiters.

Delimiter:

,

Display only names.

cut -d "," -f1 employees.csv

Output:

Alice

Bob

Charlie

David

Extract Department

cut -d "," -f2 employees.csv

Output:

Engineering

HR

Finance

Engineering

Extract Country

cut -d "," -f3 employees.csv

Output:

India

USA

UK

Germany

Extract Multiple Fields

cut -d "," -f1,2 employees.csv

Output:

Alice,Engineering

Bob,HR

Charlie,Finance

David,Engineering

Extract Field Range

cut -d "," -f2-3 employees.csv

Output:

Engineering,India

HR,USA

Finance,UK

Engineering,Germany

Character Extraction

Suppose:

Linux

Extract first three characters.

echo Linux | cut -c1-3

Output:

Lin

Extract first character.

echo Linux | cut -c1

Output:

L

Extract characters 3 to 5.

echo Kubernetes | cut -c3-5

Output:

ber

Working with /etc/passwd

Linux user information is stored in:

/etc/passwd

Example entry:

basha:x:1000:1000:Basha:/home/basha:/bin/bash

Display usernames.

cut -d ":" -f1 /etc/passwd

Display user IDs.

cut -d ":" -f3 /etc/passwd

Display login shells.

cut -d ":" -f7 /etc/passwd

Working with Command Output

Extract only usernames.

who | cut -d " " -f1

Display filesystem names.

df -h | cut -d " " -f1

Combining with Pipes

Search Engineering department.

cut -d "," -f2 employees.csv | grep Engineering

Sort countries.

cut -d "," -f3 employees.csv | sort

Remove duplicates.

cut -d "," -f2 employees.csv | sort | uniq

Count unique departments.

cut -d "," -f2 employees.csv | sort | uniq | wc -l

Common Options

Option Description
-d Specify delimiter
-f Select fields
-c Select characters
--complement Display everything except selected fields

Using --complement

Display everything except department.

cut -d "," -f2 --complement employees.csv

Output:

Alice,India

Bob,USA

Charlie,UK

David,Germany

Real Production Examples

Extract usernames.

cut -d ":" -f1 /etc/passwd

Extract mounted filesystems.

mount | cut -d " " -f3

Extract pod names.

kubectl get pods | cut -d " " -f1

Extract IP addresses.

ip addr | grep inet | cut -d " " -f6

Extract Docker image names.

docker images | cut -d " " -f1

Production Perspective

The cut command is frequently used to:

  • Process CSV reports
  • Extract usernames
  • Analyze logs
  • Parse configuration files
  • Build shell scripts
  • Process Kubernetes and Docker output

It is lightweight, fast, and commonly combined with other text-processing tools.


Hands-on Lab

Task 1

Create:

cat > students.csv

Contents:

Rahul,CSE,80

Priya,IT,90

Arjun,ECE,85

Neha,CSE,95

Task 2

Display names.

cut -d "," -f1 students.csv

Task 3

Display departments.

cut -d "," -f2 students.csv

Task 4

Display marks.

cut -d "," -f3 students.csv

Task 5

Display names and marks.

cut -d "," -f1,3 students.csv

Task 6

Display usernames.

cut -d ":" -f1 /etc/passwd

Task 7

Display login shells.

cut -d ":" -f7 /etc/passwd

Task 8

Count departments.

cut -d "," -f2 students.csv | sort | uniq | wc -l

Command Deep Dive

Command Purpose Production Example
cut -d "," -f1 Extract first field Usernames
cut -d ":" -f7 Extract login shell /etc/passwd
cut -c1-5 Extract characters IDs
cut --complement Exclude fields CSV processing

Production Troubleshooting Scenario

Scenario

A Linux administrator receives a CSV report containing server information.

server01,Running,10.0.0.10

server02,Stopped,10.0.0.11

server03,Running,10.0.0.12

server04,Maintenance,10.0.0.13

Tasks:

  1. Display server names.
  2. Display only IP addresses.
  3. Show server names and status.
  4. Count running servers.

Solutions:

cut -d "," -f1 servers.csv

cut -d "," -f3 servers.csv

cut -d "," -f1,2 servers.csv

grep Running servers.csv | wc -l

Mini Challenge

Create:

employees.csv
Alice,Engineering,India,85000

Bob,HR,USA,60000

Charlie,Finance,UK,75000

David,Engineering,Germany,90000

Perform the following:

  • Display employee names.
  • Display departments.
  • Display salaries.
  • Display names and salaries.
  • Display everything except salary.
  • Count unique departments.
  • Show only Engineering employees and their names.

Best Practices

  • Use cut only for consistently delimited data.
  • Verify the delimiter before extracting fields.
  • Combine cut with grep, sort, uniq, and wc for advanced processing.
  • Use meaningful delimiters such as commas, colons, or tabs.
  • For complex data extraction, consider awk in later lessons.

Common Mistakes

❌ Forgetting the delimiter.

✅ Incorrect:

cut -f2 employees.csv

Correct:

cut -d "," -f2 employees.csv

❌ Using cut on irregularly spaced text.

cut works best with structured, delimiter-separated files.


❌ Expecting cut to process variable-width columns.

✅ Use awk for more advanced parsing.


Interview Questions

Beginner

  1. What is the purpose of the cut command?
  2. What does -d specify?
  3. What does -f do?
  4. How do you extract the first field?

Intermediate

  1. Difference between -c and -f?
  2. Explain --complement.
  3. Why is cut useful for CSV files?
  4. When should you use awk instead of cut?

Architect Level

  1. How would you process a large CSV report containing millions of records?
  2. Why is cut commonly used in shell scripting?
  3. How would you combine cut, grep, and sort to analyze production data?

Summary

In this lesson, you learned:

  • Extracting fields using delimiters
  • Extracting character positions
  • Processing CSV files
  • Working with /etc/passwd
  • Combining cut with other text-processing tools
  • Real-world administration use cases

The cut command is a simple yet powerful utility for extracting structured data. It is commonly used in shell scripts, automation, and production environments.


Key Takeaways

  • cut extracts fields or characters from text.
  • -d specifies the delimiter.
  • -f selects fields.
  • -c selects character positions.
  • cut works best with consistently structured data.
  • Combine cut with grep, sort, uniq, and wc for efficient text processing.

What's Next?

sort Command — Sorting Text in Linux

In the next lesson, you'll learn:

  • Alphabetical sorting
  • Numeric sorting
  • Reverse sorting
  • Sorting by specific fields
  • Removing duplicates with sort
  • Practical log and report analysis