cut Command — Extracting Columns from Text¶
The
cutcommand 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. Masteringcuthelps Linux administrators and DevOps engineers quickly retrieve only the information they need.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand the
cutcommand - Extract characters from text
- Extract fields using delimiters
- Extract multiple fields
- Work with CSV files
- Process Linux configuration files
- Combine
cutwith 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:
You only need:
Instead of manually editing the file:
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:
Sample File¶
Create a file.
Contents:
Press:
Extract Fields¶
Fields are separated by delimiters.
Delimiter:
Display only names.
Output:
Extract Department¶
Output:
Extract Country¶
Output:
Extract Multiple Fields¶
Output:
Extract Field Range¶
Output:
Character Extraction¶
Suppose:
Extract first three characters.
Output:
Extract first character.
Output:
Extract characters 3 to 5.
Output:
Working with /etc/passwd¶
Linux user information is stored in:
Example entry:
Display usernames.
Display user IDs.
Display login shells.
Working with Command Output¶
Extract only usernames.
Display filesystem names.
Combining with Pipes¶
Search Engineering department.
Sort countries.
Remove duplicates.
Count unique departments.
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.
Output:
Real Production Examples¶
Extract usernames.
Extract mounted filesystems.
Extract pod names.
Extract IP addresses.
Extract Docker image names.
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:
Contents:
Task 2¶
Display names.
Task 3¶
Display departments.
Task 4¶
Display marks.
Task 5¶
Display names and marks.
Task 6¶
Display usernames.
Task 7¶
Display login shells.
Task 8¶
Count departments.
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:
- Display server names.
- Display only IP addresses.
- Show server names and status.
- 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:
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
cutonly for consistently delimited data. - Verify the delimiter before extracting fields.
- Combine
cutwithgrep,sort,uniq, andwcfor advanced processing. - Use meaningful delimiters such as commas, colons, or tabs.
- For complex data extraction, consider
awkin later lessons.
Common Mistakes¶
❌ Forgetting the delimiter.
✅ Incorrect:
Correct:
❌ 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¶
- What is the purpose of the
cutcommand? - What does
-dspecify? - What does
-fdo? - How do you extract the first field?
Intermediate¶
- Difference between
-cand-f? - Explain
--complement. - Why is
cutuseful for CSV files? - When should you use
awkinstead ofcut?
Architect Level¶
- How would you process a large CSV report containing millions of records?
- Why is
cutcommonly used in shell scripting? - How would you combine
cut,grep, andsortto analyze production data?
Summary¶
In this lesson, you learned:
- Extracting fields using delimiters
- Extracting character positions
- Processing CSV files
- Working with
/etc/passwd - Combining
cutwith 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¶
cutextracts fields or characters from text.-dspecifies the delimiter.-fselects fields.-cselects character positions.cutworks best with consistently structured data.- Combine
cutwithgrep,sort,uniq, andwcfor 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