cut and tr Commands in Linux — Extract & Transform Text
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
|), 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
| Option | Description |
|---|---|
-d C | Delimiter character (default: TAB) |
-f N | Select field N (1-indexed) |
-c N | Select character at position N |
-c N-M | Character range (positions N through M) |
--complement | Invert selection (show everything except) |
--output-delimiter=S | Output delimiter (default: same as input) |
tr Options Table
| Option | Description |
|---|---|
| SET1 SET2 | Translate each char in SET1 to corresponding char in SET2 |
-d | Delete characters in SET1 |
-s | Squeeze (collapse) repeated characters |
-c | Complement — 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.comExtracts 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
DEFExtracts 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.comShows everything except field 3 (the role column).
Example 4: tr Translate (SET1 SET2)
$ echo "hello world" | tr 'a-z' 'A-Z'
HELLO WORLDMaps 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 ' '
RemoveextraspacesDeletes 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
line3Collapses consecutive spaces (or newlines) into a single one.
Example 7: Uppercase to Lowercase
$ echo "HELLO WORLD" | tr '[:upper:]' '[:lower:]'
hello worldUsing 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/nologinExtracts 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.5Extracts IP addresses from Apache/Nginx logs, cleans brackets, counts unique IPs, and shows top 5.
Common Use Cases
| Use Case | Command |
|---|---|
| Extract email from CSV | cut -d',' -f2 users.csv |
| Normalize case | tr 'A-Z' 'a-z' < input.txt |
| Remove punctuation | tr -d '[:punct:]' < text.txt |
| Strip trailing newlines | tr -d '\n' < broken.txt |
| Convert tabs to spaces | tr '\t' ' ' < data.tsv |
| Show first 10 characters | cut -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 — buttr '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 -cworks byte-wise by default — use-bfor bytes or set locale for multi-byte support. - tr -d deletes ALL occurrences: If you only want to delete leading/trailing spaces, use
sedinstead.
Practice Exercises
- cut CSV: Extract the second and fourth columns from a CSV file.
- tr case: Convert a file to all uppercase and save to a new file.
- squeeze spaces: Read a file with double spaces and collapse them.
- cut+paste: Use
cutto extract field 3 andpasteto merge it back differently. - 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' | sortReal-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