cut

Basic Usage of cut - Extracting Specific Text from Files

The cut command is used to extract specific sections of text from files or command output. It is commonly used in shell scripting, log analysis, CSV processing, and automation tasks.

cut [OPTION]... [FILE]

Example:

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

Example output:

root
daemon
bin
sys
  • cut extracts portions of lines
  • Commonly used with structured text files
  • Frequently combined with pipes (|)
  • Very useful for shell scripting and automation

Understanding Delimiters

Most text processing with cut relies on delimiters.

Example line from /etc/passwd:

root:x:0:0:root:/root:/bin/bash

The delimiter here is:

:

Each section between delimiters is called a field.

Field breakdown:

Field Value
1 root
2 x
3 0
4 0
5 root
6 /root
7 /bin/bash

Extracting Specific Fields

To extract only usernames:

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

Breakdown:

  • -d ":" sets delimiter to colon
  • -f1 selects field number 1

Result:

root
daemon
bin
  • Very common in Linux administration
  • Useful for processing system files

Extracting Multiple Fields

To extract username and shell:

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

Example output:

root:/bin/bash
daemon:/usr/sbin/nologin
  • -f1,7 selects multiple fields
  • Fields remain separated by original delimiter

Extracting a Range of Fields

To extract multiple consecutive fields:

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

Example output:

root:x:0:0
daemon:x:1:1
  • 1-4 means fields 1 through 4
  • Useful for structured data extraction

Extracting Characters Instead of Fields

The cut command can also extract character positions.

Example:

echo "LinuxServer" | cut -c1-5

Output:

Linux

Breakdown:

  • -c means character positions
  • 1-5 selects characters 1 through 5

Extracting Single Characters

To extract specific characters:

echo "abcdef" | cut -c2,4

Output:

bd
  • Extracts characters 2 and 4
  • Useful for fixed-width text processing

Using cut with Pipes

The cut command is commonly combined with pipes.

Example:

df -h | cut -c1-20
  • df -h generates disk usage output
  • cut -c1-20 extracts first 20 characters
  • Useful for formatting and filtering output

Processing CSV Files

Example CSV file:

name,email,role
john,[email protected],admin
anna,[email protected],user

Extract email column:

cut -d "," -f2 users.csv

Output:

  • , is CSV delimiter
  • Common for data processing tasks

Ignoring Lines Without Delimiters

By default, lines without delimiters are printed unchanged.

To suppress them:

cut -d ":" -f1 --only-delimited file.txt
  • --only-delimited ignores malformed lines
  • Useful for cleaner parsing

Changing Output Delimiter

By default, original delimiters are preserved.

To change output delimiter:

cut -d ":" -f1,7 --output-delimiter=" -> " /etc/passwd

Example output:

root -> /bin/bash
daemon -> /usr/sbin/nologin
  • Improves readability
  • Useful in reporting scripts

Combining cut with Other Commands

Extract usernames currently logged in:

who | cut -d " " -f1

Extract first column from process list:

ps aux | cut -c1-10

Extract filesystem names:

df -h | cut -d " " -f1
  • cut is extremely common in shell pipelines
  • Useful for automation and reporting

Combining Multiple Options

Example:

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

This combines:

  • -d for delimiter
  • -f for field selection

Another example:

cut -c1-15 file.txt

This extracts:

  • character positions 1 through 15

Practical Script Example (Step-by-Step Explanation)

Script

#!/bin/bash

FILE="/etc/passwd"

echo "System users and login shells:"

cut -d ":" -f1,7 $FILE

Step 1: Shebang

#!/bin/bash
  • Defines Bash interpreter
  • Ensures script runs using Bash shell

Step 2: Defining file variable

FILE="/etc/passwd"
  • Stores file path in variable
  • Makes script easier to maintain

Example value:

/etc/passwd

Step 3: Displaying informational message

echo "System users and login shells:"
  • Prints readable message
  • Helps structure script output

Example output:

System users and login shells:

Step 4: Extracting selected fields

cut -d ":" -f1,7 $FILE

Breakdown:

  • -d ":" defines colon delimiter
  • -f1,7 extracts fields 1 and 7
  • $FILE expands variable value

Example input line:

root:x:0:0:root:/root:/bin/bash

Extracted result:

root:/bin/bash

What this script does

Step-by-step flow:

  1. Defines target file
  2. Displays informational message
  3. Extracts usernames and login shells
  4. Prints formatted results

Why this matters in production

This type of processing is useful for:

  • user account audits
  • configuration parsing
  • CSV processing
  • automation scripts
  • system reporting

The cut command is heavily used in:

  • DevOps workflows
  • shell scripting
  • monitoring systems
  • Linux administration

Common Beginner Mistakes

Using wrong delimiter:

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

This fails because /etc/passwd uses : not ,.

Correct version:

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

Another mistake:

Using fields on space-separated text with inconsistent spacing:

ps aux | cut -d " " -f1

This may produce unreliable output because multiple spaces exist.

In such cases, tools like awk are often better.


Summary

In this guide, you learned:

  • how to extract fields using cut
  • how delimiters work
  • extracting multiple fields
  • extracting ranges
  • character-based extraction
  • CSV processing
  • using cut with pipes
  • output delimiter customization
  • practical shell scripting with cut

These skills are essential for:

  • Linux administration
  • shell scripting
  • text processing
  • automation
  • data extraction

Additional cut parameters not covered in this guide include:

--complement: Select all except specified fields
-s: Suppress lines without delimiters
-z: Use null-terminated lines
--help: Display help information
--version: Display version information