Table of Contents

  1. Table of Contents
  2. Regex Cheatsheet
    1. Basic Syntax
    2. Common Escaped Characters
    3. Character Classes
    4. Assertions
    5. Common Examples
  3. Advanced sed + Regex Snippets Cheatsheet
    1. Append a Link to the End of All Markdown Headers
    2. Replace “foo” with “bar” Only in Lines Starting with ##
    3. Insert a Line After Every Level-1 (#) Header
    4. Add a “Back to Top” Link After All ## Headers
    5. Prepend a Tag to All Headers (Any Level)
    6. Match Headers with Optional Whitespace and Append a Link
    7. Remove Trailing Spaces from All Lines
    8. Insert “NEW HEADER” Before All ## Headers
    9. Backup & Replace in One Go
    10. Combine Multiple Modifications
    11. Key sed Concepts

Regex Cheatsheet

Regular expressions (regex) are patterns used to match character combinations in strings.

Basic Syntax

Pattern Description
. Any character except newline
^ Start of string
$ End of string
* 0 or more of preceding element
+ 1 or more of preceding element
? 0 or 1 of preceding element
{n} Exactly n of preceding element
{n,} n or more of preceding element
{n,m} Between n and m of preceding element
| OR
() Grouping
[] Character class
[^ ] Negated character class
\ Escape special character

Common Escaped Characters

Pattern Description
\d Digit (0-9)
\D Non-digit
\w Word character (alphanumeric & underscore)
\W Non-word character
\s Whitespace
\S Non-whitespace
\b Word boundary
\B Non-word boundary
\\ Literal backslash

Character Classes

Pattern Description
[abc] Match ‘a’, ‘b’, or ‘c’
[a-z] Match lowercase letters
[A-Z] Match uppercase letters
[0-9] Match digits
[a-zA-Z0-9] Match alphanumeric characters
[^abc] Match any character except ‘a’,’b’,’c’

Assertions

Pattern Description
(?=...) Positive lookahead
(?!...) Negative lookahead
(?<=...) Positive lookbehind
(?<!...) Negative lookbehind

Common Examples

Pattern Description
^hello String starts with “hello”
world$ String ends with “world”
\bword\b Matches the whole word “word”
\d{3}-\d{2}-\d{4} Social Security Number pattern
^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}$ Email pattern

Advanced sed + Regex Snippets Cheatsheet

This cheatsheet covers advanced sed use-cases with powerful regular expressions for modifying files.


Matches headers starting with either # or ## and appends a [🔗](#link) at the end of each.

sed -E '/^#{1,2} /s/$/ [🔗](#link)/' file.md

Replace “foo” with “bar” Only in Lines Starting with ##

sed -E '/^## /s/foo/bar/g' file.md

Insert a Line After Every Level-1 (#) Header

sed -E '/^# /a\
This is an inserted line.
' file.md

sed -E '/^## /a\
[Back to Top](#top)
' file.md

Prepend a Tag to All Headers (Any Level)

sed -E '/^#+ /s/^/(TAG) /' file.md

Handles # Header, ## Header, and even # Header.

sed -E '/^#{1,2}[[:space:]]+/s/$/ [🔗](#link)/' file.md

Remove Trailing Spaces from All Lines

sed -E 's/[[:space:]]+$//' file.md

Insert “NEW HEADER” Before All ## Headers

sed -E '/^## /i\
NEW HEADER
' file.md

Backup & Replace in One Go

Replaces foo with bar in-place, making a .bak backup.

sed -E -i.bak 's/foo/bar/g' file.md

Combine Multiple Modifications

Append a link to level-1 or level-2 headers, remove trailing spaces, and add a tag to each header.

sed -E '
  /^#{1,2} /s/$/ [🔗](#link)/
  s/[[:space:]]+$//
  /^#+ /s/^/(TAG) /
' file.md

Key sed Concepts

  • -E: Extended regex (no need to escape +, ?, {}).
  • /pattern/: Match lines containing pattern.
  • s/pattern/replacement/: Substitute matched pattern.
  • a\, i\: Append/Insert lines.
  • -i: In-place editing.

This cheatsheet is a quick reference for advanced sed with regex patterns.