cut, sort & uniq

Three small tools to slice columns, order lines and remove duplicates.

Syntaxcut -d DELIM -f N sort [-n] [-r] uniq [-c]

These classic filters are usually chained with pipes.

cut — slice columns

cut extracts parts of each line, by character or by delimited field.

  • cut -d: -f1 /etc/passwd — field 1, using : as the separator.

sort — order lines

  • sort — alphabetical order.
  • sort -n — numeric order.
  • sort -r — reverse.

uniq — collapse duplicates

uniq removes adjacent duplicate lines, so you almost always sort first. Add -c to count how many times each line appeared.

Example

Example · bash
# Extract just the usernames from the accounts file
cut -d: -f1 /etc/passwd

# Sort numbers correctly (not as text)
sort -n sizes.txt

# Count how often each line appears, biggest first
sort access.log | uniq -c | sort -nr | head

When to use it

  • A developer extracts the third column from a CSV with 'cut -d, -f3 data.csv' to isolate email addresses for export.
  • A sysadmin sorts process names alphabetically with 'ps -eo comm | sort | uniq -c | sort -rn' to see which programs have the most processes.
  • A log analyst pipes 'cut -d" " -f1 access.log | sort | uniq -c | sort -rn | head' to rank top visitors by IP.

More examples

Extract fields with cut

Uses cut -d to set the delimiter and -f to select fields, or -c to select character positions.

Example · bash
# Extract username (field 1) from /etc/passwd
cut -d: -f1 /etc/passwd
# root
# daemon
# alice

# Extract first 10 characters of each line
cut -c1-10 /var/log/syslog

Sort lines numerically and reverse

Shows sort with -n for numeric order, -r to reverse, and -k to sort by a specific column.

Example · bash
# Sort numbers correctly (not lexicographically)
sort -n sizes.txt

# Sort in reverse numerical order
sort -rn sizes.txt

# Sort by second column
sort -k2 -n data.txt

Count and deduplicate with uniq

Chains cut, sort, and uniq -c to count how many times each value appears, then ranks by frequency.

Example · bash
# List unique shells in use (must sort first)
cut -d: -f7 /etc/passwd | sort | uniq

# Count occurrences of each shell
cut -d: -f7 /etc/passwd | sort | uniq -c | sort -rn
#   30 /usr/sbin/nologin
#    3 /bin/bash

Discussion

  • Be the first to comment on this lesson.
cut, sort & uniq — Linux | SoundsCode