Linux cut, sort, uniq, tr, paste, join, and comm Cheat Sheet

Column extraction, sorting, deduplication, character translation, file combining, and list comparison.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## cut and paste
Extract comma-delimited fields
cut -d, -f1,3 users.csv

# Print the first and third fields from a CSV-like file.

Extract character positions
cut -c1-8 ids.txt

# Print characters 1 through 8 from each line.

Exclude selected fields
cut -d, --complement -f2 users.csv

# Print all fields except one.

Merge files side by side
paste names.txt emails.txt

# Combine lines from two files into columns.

Paste with a custom delimiter
paste -d, names.txt emails.txt

# Combine files using commas or tabs.

## sort and uniq
Sort lines alphabetically
sort names.txt

# Default lexicographic sort.

Sort in reverse order
sort -r names.txt

# Invert the output order.

Sort numerically
sort -n numbers.txt

# Order lines by numeric value.

Sort human-readable sizes
sort -h sizes.txt

# Sort values like 1K, 5M, 2G.

Sort by a specific field
sort -k2,2 file.txt

# Sort on the second whitespace-separated column.

Sort and deduplicate
sort -u names.txt

# Return sorted unique lines.

Count duplicate lines
sort access.log | uniq -c

# Count occurrences of adjacent duplicate lines.

Show duplicate lines only
sort file.txt | uniq -d

# Print only lines that appear more than once.

## tr Translation and Character Operations
Convert lowercase to uppercase
tr '[:lower:]' '[:upper:]' < input.txt

# Translate characters from lower to upper case.

Convert uppercase to lowercase
tr '[:upper:]' '[:lower:]' < input.txt

# Translate text to lowercase.

Delete characters
tr -d '0-9' < file.txt

# Remove all digits from a stream.

Join lines into one line
tr '
' ' ' < lines.txt

# Replace newlines with spaces.

Collapse repeated blank lines
tr -s '
' < file.txt

# Squeeze consecutive newline characters.

## join and comm
Join two files on a common field
join users.txt departments.txt

# Merge records based on matching first fields.

Join on a different field number
join -1 2 -2 1 left.txt right.txt

# Specify alternate join fields.

Show common lines between sorted files
comm -12 <(sort file1.txt) <(sort file2.txt)

# Print only lines present in both files.

Show lines only in the first file
comm -23 <(sort file1.txt) <(sort file2.txt)

# List lines unique to the first sorted file.

Show lines only in the second file
comm -13 <(sort file1.txt) <(sort file2.txt)

# List lines unique to the second sorted file.

Recommended next

No recommendations yet.