Skip to main content
Regular expressions (regex) let you describe text patterns to match, filter, and extract information quickly. Instead of scanning hundreds of log lines or manually checking usernames, regex lets you find exactly what you need in seconds. A minimal example:
This pattern matches any sequence of characters (including the empty string). We’ll expand from these basics to practical uses with common Unix tools. When we refer to regular expressions here, we mean using them with standard Unix command-line tools:
  • grep — search files or command output for matching lines (often your first choice).
  • sed — stream editor for automated search-and-replace, deleting lines, and transforming text in pipelines.
  • awk — pattern-based processing for structured, field-oriented data (useful for files like /etc/passwd).
A dark-themed slide titled "Regular Expressions" showing three numbered cards for the tools "grep", "sed", and "awk." Each card briefly describes grep as searching for patterns, sed as modifying/replacing text streams, and awk as processing structured data and fields.

Core regex symbols and examples

Below are the most useful regex metacharacters when working with grep/sed/awk, plus short examples showing how they behave. Notes:
  • Use grep -E (or egrep) to enable extended regular expressions so constructs like | (alternation) and {n} work without backslashes.
  • In POSIX grep (basic regex) some metacharacters must be escaped (e.g., \{3\}), so prefer -E for readability.

Short examples in context

  • Anchor to the start:
  • Anchor to the end (match shells that end with bash):
  • Match any single character:
  • Zero or more of previous token:
  • Any number of any characters between two tokens:
  • Character class (one of listed characters):
  • Negated character class (no digits):
  • Alternation with extended regex:
  • Exact repetition (three digits in a row):
You can copy this consolidated set of commands for quick reference:
Always quote your regex patterns (single or double quotes) to prevent the shell from interpreting special characters and to ensure the regex is passed intact to the tool.

Practical tips and next steps

  • Combine these regex patterns with shell pipelines and scripting to automate log filtering, field extraction, and bulk edits.
  • Use awk when you need field-aware processing (e.g., splitting /etc/passwd on : and extracting specific fields).
  • Use sed for in-place transformations (search-and-replace) across many files or streamed input.
This guide focused on regex usage in Bash with grep/sed/awk — pairing these patterns with conditionals and loops in scripts lets you build repeatable, reliable text-processing workflows.
Dark-themed presentation slide titled "Summary." It lists three bullet points: Bash scripts automate tasks, conditionals and loops add logic and control flow, and regular expressions help filter, search, and extract data.

Watch Video

Practice Lab