Phawat
Back to Blog

Learned on 25 Feb 2026

UNIX basic1

Quick reference notes for regex basics, grep, wc, tr, cut, sort, uniq, and common command-line patterns.

UnixCLI

UNIX basic1

Regex basics

  • *: zero or more
  • +: one or more
  • ?: zero or one
  • .: any single character
  • [^ ]: except
  • ^: start
  • $: end
  • {n}: n repetitions
  • {n,m}: n to m repetitions
  • {,m}: m or fewer repetitions

grep

  • grep -i: ignore upper/lower case
  • grep -v: invert / not match
  • grep -c: count of matching lines
  • grep -w: whole word only

wc

  • wc: word counter
  • wc -c: characters
  • wc -w: words
  • wc -l: lines
  • Default output includes lines, words, and characters

tr

  • tr sourceChars destChars
  • tr 'A-Z' 'a-z'
  • tr -d '0-9': delete 0-9
  • tr aeiou 12333: maps aeiou to 12333

head / tail

  • head / tail: default 10 lines
  • head -n {number} / tail -n {number}
  • Combine them:
head -n 100 | tail -n 20

Character set example

  • [a] = a

cut

  • cut -f: tab-separated fields
  • cut -c: characters
  • cut -d: custom delimiter

Examples:

cut -f1,4
cut -d'|' -f1-3
cut -c1-5

sort

  • sort -r: descending order
  • sort -n: numeric sort
  • sort -d: dictionary order
  • sort -t: delimiter
  • sort -k: column/key

Example:

sort -nr -k3

Meaning: sort by the 3rd column in descending numeric order.

uniq

  • uniq -c: count duplicated lines
  • uniq -d: print one copy of duplicated lines
  • uniq -u: print lines that occur only once

Regex examples

[ab]+   -> a, b, aa, abb
(ab)+   -> ab, abab, ababab

To update later

  • Add small input/output examples for each command
  • Add notes on when to combine sort | uniq