Skip to content
cut and tr Commands in Linux — Extract & Transform Text

cut and tr Commands in Linux — Extract & Transform Text

DodaTech Updated Jun 20, 2026 5 min read

The cut and tr commands extract and transform text in Linux — pulling specific columns from CSV files, converting case, removing unwanted characters, and squeezing repeated whitespace. They’re essential tools in every sysadmin’s text-processing toolkit.

What You’ll Learn

By the end of this tutorial, you’ll extract fields by delimiter and character position, translate character sets, delete unwanted characters, squeeze repeats, and build text-processing pipelines combining cut, tr, sort, and uniq.

Why cut and tr Matter

Raw log data is messy — inconsistent delimiters, mixed case, extra whitespace. cut extracts the fields you need, and tr normalizes the rest. DodaZIP uses cut to parse archive headers and tr to sanitize filenames. Durga Antivirus Pro transforms threat signatures to lowercase before comparison using tr '[:upper:]' '[:lower:]'.

Learning Path

    flowchart LR
  A[Text Processing Tools] --> B[sort & uniq]
  B --> C[cut & tr<br/>You are here]
  C --> D[awk & sed]
  C --> E[grep Patterns]
  style C fill:#f90,color:#fff
  
Prerequisites: Familiarity with piping (|), essential Linux commands, and basic Bash syntax.

Syntax Overview

cut OPTION [file...]
tr [OPTION] SET1 [SET2]

cut extracts columns. tr translates, deletes, or squeezes characters.

cut Options Table

OptionDescription
-d CDelimiter character (default: TAB)
-f NSelect field N (1-indexed)
-c NSelect character at position N
-c N-MCharacter range (positions N through M)
--complementInvert selection (show everything except)
--output-delimiter=SOutput delimiter (default: same as input)

tr Options Table

OptionDescription
SET1 SET2Translate each char in SET1 to corresponding char in SET2
-dDelete characters in SET1
-sSqueeze (collapse) repeated characters
-cComplement — operate on characters NOT in SET1
[:upper:]All uppercase letters
[:lower:]All lowercase letters
[:digit:]All digits
[:space:]All whitespace
[:punct:]All punctuation

Examples

Example 1: cut by Delimiter (-d -f)

$ cat users.csv
name,email,role
alice,alice@example.com,admin
bob,bob@test.com,user

$ cut -d',' -f1,2 users.csv
name,email
alice,alice@example.com
bob,bob@test.com

Extracts columns 1 and 2 from a comma-separated file.

Example 2: cut by Character Range (-c)

$ cat ids.txt
ABC123
XYZ789
DEF456

$ cut -c1-3 ids.txt
ABC
XYZ
DEF

Extracts characters 1-3 from each line — useful for fixed-width records.

Example 3: cut with Complement

$ cut -d',' --complement -f3 users.csv
name,email
alice,alice@example.com
bob,bob@test.com

Shows everything except field 3 (the role column).

Example 4: tr Translate (SET1 SET2)

$ echo "hello world" | tr 'a-z' 'A-Z'
HELLO WORLD

Maps lowercase to uppercase. Works character-by-character.

Example 5: tr Delete (-d)

$ echo "user:pass:1234" | tr -d ':'
userpass1234

$ echo "Remove   extra   spaces" | tr -d ' '
Removeextraspaces

Deletes all specified characters from the input.

Example 6: tr Squeeze (-s)

$ echo "too    many    spaces" | tr -s ' '
too many spaces

$ cat messy.txt
line1

line2


line3
$ tr -s '\n' < messy.txt
line1
line2
line3

Collapses consecutive spaces (or newlines) into a single one.

Example 7: Uppercase to Lowercase

$ echo "HELLO WORLD" | tr '[:upper:]' '[:lower:]'
hello world

Using POSIX character classes is more readable and locale-safe than a-z ranges.

Example 8: tr with Classes — Keep Only Digits

$ echo "Order #12345 total $89.99" | tr -cd '[:digit:]\n'
123458999

$ echo "Order #12345 total $89.99" | tr -cd '[:digit:].\n'
12345.89.99

-c complements the set, -d deletes — together they keep only what you want.

Example 9: cut + tr Pipeline

$ cat /etc/passwd | cut -d':' -f1,7 | tr ':' '\t'
root    /bin/bash
daemon  /usr/sbin/nologin
www-data /usr/sbin/nologin

Extracts username and shell from /etc/passwd, replaces the colon separator with a tab.

Example 10: Full Pipeline — Parse Access Log

$ cat access.log | cut -d' ' -f1 | tr -d '[]' | sort | uniq -c | sort -rn | head -5
   45 192.168.1.1
   32 10.0.0.2
   28 192.168.1.5

Extracts IP addresses from Apache/Nginx logs, cleans brackets, counts unique IPs, and shows top 5.

Common Use Cases

Use CaseCommand
Extract email from CSVcut -d',' -f2 users.csv
Normalize casetr 'A-Z' 'a-z' < input.txt
Remove punctuationtr -d '[:punct:]' < text.txt
Strip trailing newlinestr -d '\n' < broken.txt
Convert tabs to spacestr '\t' ' ' < data.tsv
Show first 10 characterscut -c1-10 file.txt

Common Errors

  • cut -f without -d: If the file uses commas but you don’t specify -d',', cut defaults to TAB and returns the whole line.
  • tr with overlapping ranges: tr 'a-z' 'A-Z' works — but tr 'a-z' '0-9' replaces 26 letters with only 10 digits, repeating the last digit.
  • tr only on stdin: tr does NOT accept filenames as arguments — always redirect (< file) or pipe.
  • cut character ranges with multi-byte UTF-8: cut -c works byte-wise by default — use -b for bytes or set locale for multi-byte support.
  • tr -d deletes ALL occurrences: If you only want to delete leading/trailing spaces, use sed instead.

Practice Exercises

  1. cut CSV: Extract the second and fourth columns from a CSV file.
  2. tr case: Convert a file to all uppercase and save to a new file.
  3. squeeze spaces: Read a file with double spaces and collapse them.
  4. cut+paste: Use cut to extract field 3 and paste to merge it back differently.
  5. pipeline: Extract IPs from a log, remove duplicate lines, count, and sort.

Challenge

Write a one-liner that reads /etc/passwd, extracts usernames and home directories, converts delimiters to tabs, and sorts by username. This is a pattern Durga Antivirus Pro uses when auditing user accounts during security scans.

cut -d':' -f1,6 /etc/passwd | tr ':' '\t' | sort

Real-World Task

A web application logs data as [2026-06-20 10:30:45] ERROR: user_id=42 action=login. Extract timestamps and user IDs into a tab-separated format, then count actions per user.

What is cut?

The cut command extracts specific columns or character ranges from each line of a file, using a delimiter or fixed character positions.

What is tr?

The tr (translate) command transforms text by replacing, deleting, or squeezing characters from stdin based on specified character sets or POSIX classes.

Related Tutorials

  • Essential Linux Commands — text processing overview
  • sort and uniq — use with cut+tr in pipelines
  • Bash Scripting Guide — automate text processing
  • awk & sed — advanced text transformation tools

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro