> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Using Regular Expressions

> Guide to using regular expressions with Unix command line tools grep sed and awk for searching filtering and transforming text in shell scripts and pipelines

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:

```text theme={null}
(.*)
```

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`).

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/pGYwDVd7uFSlinOw/images/Prep-Course-Red-Hat-Certified-System-Administrator-RHCSA/Enhanced-Command-Line/Using-Regular-Expressions/regular-expressions-grep-sed-awk.jpg?fit=max&auto=format&n=pGYwDVd7uFSlinOw&q=85&s=585a53635f9c602fd0784e9204019781" alt="A dark-themed slide titled &#x22;Regular Expressions&#x22; showing three numbered cards for the tools &#x22;grep&#x22;, &#x22;sed&#x22;, and &#x22;awk.&#x22; Each card briefly describes grep as searching for patterns, sed as modifying/replacing text streams, and awk as processing structured data and fields." width="1920" height="1080" data-path="images/Prep-Course-Red-Hat-Certified-System-Administrator-RHCSA/Enhanced-Command-Line/Using-Regular-Expressions/regular-expressions-grep-sed-awk.jpg" />
</Frame>

## 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.

| Symbol | Meaning | Example |
| - | - | - |
| `^` | Anchor to the start of the line | `grep "^root" /etc/passwd` — lines starting with `root` |
| `$` | Anchor to the end of the line | `grep "bash$" /etc/passwd` — shells ending with `bash` |
| `.` | Any single character (except newline) | `grep "r.ot" file.txt` — matches `root`, `r1ot`, `r-ot` |
| `*` | Zero or more of the preceding token | `grep "ro*t" file.txt` — `rt`, `rot`, `root`, `rooot` |
| `.*` | Any number of any characters (greedy) | `grep "r.*t" file.txt` — `rt`, `root`, `randomtextt` |
| `[...]` | Character class — any one of the characters | `grep "user[12]" file.txt` — `user1` or `user2` |
| `[^...]` | Negated character class — any character not listed | `grep "[^0-9]" file.txt` — lines with a non-digit character |
| `-E` (grep) | Extended regex mode (or use `egrep`) | `grep -E "root\|student" /etc/passwd` — `root` OR `student` |
| `` `{n}` `` | Quantifier: exact repetition of previous token | `grep -E "[0-9]{3}" file.txt` — any three consecutive digits |

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:

```bash theme={null}
grep "^root" /etc/passwd
```

* Anchor to the end (match shells that end with `bash`):

```bash theme={null}
grep "bash$" /etc/passwd
```

* Match any single character:

```bash theme={null}
grep "r.ot" file.txt
```

* Zero or more of previous token:

```bash theme={null}
grep "ro*t" file.txt
```

* Any number of any characters between two tokens:

```bash theme={null}
grep "r.*t" file.txt
```

* Character class (one of listed characters):

```bash theme={null}
grep "user[12]" file.txt
```

* Negated character class (no digits):

```bash theme={null}
grep "[^0-9]" file.txt
```

* Alternation with extended regex:

```bash theme={null}
grep -E "root|student" /etc/passwd
```

* Exact repetition (three digits in a row):

```bash theme={null}
grep -E "[0-9]{3}" file.txt
```

You can copy this consolidated set of commands for quick reference:

```bash theme={null}
grep "^root" /etc/passwd
grep "bash$" /etc/passwd
grep "r.ot" file.txt
grep "ro*t" file.txt
grep "r.*t" file.txt
grep "user[12]" file.txt
grep "[^0-9]" file.txt
grep -E "root|student" /etc/passwd
grep -E "[0-9]{3}" file.txt
```

<Callout icon="lightbulb" color="#1CB2FE">
  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.
</Callout>

## 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.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/pGYwDVd7uFSlinOw/images/Prep-Course-Red-Hat-Certified-System-Administrator-RHCSA/Enhanced-Command-Line/Using-Regular-Expressions/summary-bash-scripts-conditionals-loops-regex.jpg?fit=max&auto=format&n=pGYwDVd7uFSlinOw&q=85&s=f991801632b5ad10eda00a45d6c14744" alt="Dark-themed presentation slide titled &#x22;Summary.&#x22; 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." width="1920" height="1080" data-path="images/Prep-Course-Red-Hat-Certified-System-Administrator-RHCSA/Enhanced-Command-Line/Using-Regular-Expressions/summary-bash-scripts-conditionals-loops-regex.jpg" />
</Frame>

## Links and references

* [grep manual (GNU)](https://www.gnu.org/software/grep/manual/grep.html)
* [sed — Stream Editor (GNU sed manual)](https://www.gnu.org/software/sed/manual/sed.html)
* [awk — GNU Awk User’s Guide](https://www.gnu.org/software/gawk/manual/gawk.html)
* [Regular Expressions (MDN) — reference and examples](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/red-hat-certified-system-administrator-rhcsa/module/91ecb13c-9bbb-4277-b717-7a725b4b0df7/lesson/17a5f148-26f2-41eb-9a60-e614471bae01" />

  <Card title="Practice Lab" icon="flask-conical" cta="Learn more" href="https://learn.kodekloud.com/user/courses/red-hat-certified-system-administrator-rhcsa/module/91ecb13c-9bbb-4277-b717-7a725b4b0df7/lesson/d12d150f-03e0-49a2-b7fb-1a13e286cfb0" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.