CodeFaster

CodeFaster

Share this post

CodeFaster
CodeFaster
Mastering sed: Find and Replace

Mastering sed: Find and Replace

Tyler Adams's avatar
Tyler Adams
Aug 04, 2020
∙ Paid

Share this post

CodeFaster
CodeFaster
Mastering sed: Find and Replace
1
Share

In this tutorial, we’ll explore one of sed’s most powerful features: find-and-replace. It assumes a basic familiarity with sed.

Simple find and replace

Example

echo -e 'find\nfind\nfind' | sed 's/find/replace/'

Output

replace replace replace

Explanation

Let’s break it down:

s is the command.

/find/replace/ is the option. It means any string "find" should be changed with "replace"

Simple find and replace only on the 2nd line

Example

echo -e 'find\nfind\nfind' | sed '2 s/find/replace/'

Output

find replace find

Explanation

The only difference in the command and the simple case is that there’s a "2" before the s. This sets the address of the command to just the 2nd line.

Global find and replace

Example

echo 'find find find' | sed 's/find/replace/g'

Output

replace replace replace

Explanation

The only difference in the command and the simple case is that there’s a "g" after the last slash. This makes the find and replace apply to all instances.

2nd match find and replace

Example

echo 'find find find' | sed 's/find…

This post is for paid subscribers

Already a paid subscriber? Sign in
© 2025 Tyler Adams
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share