sed step by step

 

If you have ever needed to change text inside a file on Linux and felt unsure how to do it safely, this guide is for you.

This post walks through the simplest working way to use sed, step by step. We focus on one practical task, use clear examples, and follow the same workflow used in real IT environments.

No theory overload.
No guessing.
Just small, safe steps that build confidence.


What sed Is Used For

sed is a tool that reads text, makes a change, and shows the result.

It does not open a text editor.
It does not prompt you for input.
It processes text line by line and outputs the result.

Many beginners struggle because they try to learn everything at once.
We will not do that here.

We will focus on one job only: replacing text.


Create a Simple Test File

Before using sed, we need a file to work with.

cat > servers.txt

Your terminal will look like it is waiting. That is normal.

Type the following lines, then press Ctrl + D:

server1 is online server2 is offline server3 is offline

This file is intentionally simple.
It allows you to focus on learning sed without extra distractions.


View the File Before Making Changes

Before changing anything, always check the file.

cat servers.txt

Output:

server1 is online server2 is offline server3 is offline

This is our starting point.
Right now, two servers are marked as offline.


The Simplest Working sed Pattern

The most important pattern to learn first looks like this:

sed 's/old/new/'

Here is what that means:

  • s means substitute

  • old is the text you want to find

  • new is the text you want to replace it with

This pattern is the foundation for most sed usage.


Replace Text Without Changing the File

Now we will use sed to preview a change.

sed 's/offline/online/' servers.txt

Output:

server1 is online 
server2 is online
server3 is online

What just happened:

  • sed read the file

  • it replaced the first occurrence of offline on each line

  • it printed the result to the screen

Important:
The file itself was not changed. This is a safety feature and is ideal for learning.


Confirm the File Was Not Modified

Always verify.

cat servers.txt

Output:

server1 is online 
server2 is offline
server3 is offline

Nothing changed.
This is why sed is safe to practice with.


Replace All Matches on a Line

By default, sed replaces only the first match per line.

To replace all matches, add a g.

g means global.

sed 's/offline/online/g' servers.txt

Output:

server1 is online 
server2 is online
server3 is online

In this file, the result looks the same.
The habit still matters.

When you want every match replaced, always include g.


Make the Change Permanent

So far, we have only previewed changes.

To update the file itself, use the -i option.

As a beginner, always run sed once without -i first.

-i means in place.

sed -i 's/offline/online/g' servers.txt

This time, the file is changed.
There is no undo.

That is why previewing comes first.


Verify the Final Result

cat servers.txt

Output:

server1 is online server2 is online server3 is online

This is the standard IT workflow:

Preview first.
Apply second.
Verify last.


Using sed with Command Output

sed is not limited to files.
It also works with command output.

echo "status=offline" | sed 's/offline/online/'

Output:

status=online

The pipe (|) takes the output of one command and passes it into the next.

This pattern is common in scripts and automation, where output needs to be cleaned up before use.


Common Beginner Mistakes

Forgetting Quotes

This can break or behave unpredictably:

sed s/offline/online/ servers.txt

Always use quotes:

sed 's/offline/online/' servers.txt

Using -i Too Early

Beginners often modify files by accident.

If you are learning, never use -i first.
Preview the result before applying changes.


Typos in the Search Text

sed is exact.

If the text does not match exactly, nothing happens.
Check spelling and letter case.


A Real IT Example

Imagine a configuration file that contains:

ENV=dev

To promote this to production:

sed -i 's/ENV=dev/ENV=prod/' app.conf

This is how environment changes, feature flags, and service settings are updated safely and consistently in real systems.


A Small Optional Next Step

You can target a specific line number.

sed '2s/online/offline/' servers.txt

This changes text only on line 2.

You do not need this yet.
Just know that it exists when you are ready.


Final Thoughts

sed becomes easier when you use it for one small task at a time.

Work through these examples a few times.
With repetition, the commands will start to feel natural.

Confidence comes from practice, not memorization.