Skip to content

SED Basics

sed is a stream editor. I usually use grep for filtering contents, sed for editing texts, and awk for processing some tabular data.

1. Preparation

Raw Data

Run the following command to save the data.

cat <<MYORDER> myorder.txt
Item        Status
Keyboard    Shipping
Mouse       Pending
Monitor     Wait For Payment
Laptop      Payment Complete
Lamp        Out Of Stock
MYORDER
cat <<SONG> song.txt
On the twelfth day of Christmas my true love gave to me
twelve drummers drumming
eleven pipers piping
ten lords a leaping
nine ladies dancing
eight maids a milking
seven swans a swimming
six geese a laying
five golden rings
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree
SONG

A sed script is usually made of three parts: condition, action, and option.

For example, if we want to substitute e with a in line 1 to line 3:

sed '1,3s/e/a/g' song.txt

Here,

  • 1,3 is the condition: from line 1 to line 3.
  • s is the action: substitute
  • /e/a/g is the option for substitution

2. Let's Play!

2.1 Change the Status

Monitor

Let's change the monitor's status to Payment Complete

sed 's/Wait For Payment/Payment Complete/' myorder.txt
Item        Status
Keyboard    Shipping
Mouse       Pending
Monitor     Payment Complete
Laptop      Payment Complete
Lamp        Out Of Stock

However, the change is not made to the file. In order to edit the file directly, we need to use inplace mode.

Monitor

sed -i 's/Wait For Payment/Payment Complete/' myorder.txt
cat myorder.txt

Output

Item        Status
Keyboard    Shipping
Mouse       Pending
Monitor     Payment Complete
Laptop      Payment Complete
Lamp        Out Of Stock

Monitor, Laptop

Now let's change the status of Monitor and Laptop to Pending.

We need to change 2 lines now.

sed -i 's/Payment Complete/Pending/' myorder.txt
cat myorder.txt

Output

Item        Status
Keyboard    Shipping
Mouse       Pending
Monitor     Pending
Laptop      Pending
Lamp        Out Of Stock

Mouse, Monitor

Now, let's change the status of Mouse and Monitor to Shipping.

Here, we just try different commands and check out the results, and we don't want inplace changes.

The following command will change the status from line 3 to line 4.

sed '3,4 s/Pending/Shipping/' myorder.txt

The following command will change the status in lines starts with Mouse or Monitor

sed -E '/^(Mouse|Monitor)/ s/Pending/Shipping/' myorder.txt

Drum or swim?

Let's try to replace the word drum with swim in the song.

Run

sed 's/drum/swim/' song.txt

Output

However, only one occurence of drum is replaced

On the twelfth day of Christmas my true love gave to me
twelve swimmers drumming
eleven pipers piping
ten lords a leaping
nine ladies dancing
eight maids a milking
seven swans a swimming
six geese a laying
five golden rings
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree

Let's try to replace all the word drum with swim in the song.

Run

sed 's/drum/swim/g' song.txt

Output

Now all the occurences of drum are replaced

On the twelfth day of Christmas my true love gave to me
twelve swimmers swimming
eleven pipers piping
ten lords a leaping
nine ladies dancing
eight maids a milking
seven swans a swimming
six geese a laying
five golden rings
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree

2.2 Remove a Line

Remove Lines

Run

sed '3,5d' song.txt

Output

On the twelfth day of Christmas my true love gave to me
twelve drummers drumming
eight maids a milking
seven swans a swimming
six geese a laying
five golden rings
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree

Run

sed '/^t/d' song.txt

Output

On the twelfth day of Christmas my true love gave to me
eleven pipers piping
nine ladies dancing
eight maids a milking
seven swans a swimming
six geese a laying
five golden rings
four calling birds
and a partridge in a pear tree

2.3 Insert a Line

Insert a Line

Run

sed '3i I am an inserted line.' song.txt

Output

On the twelfth day of Christmas my true love gave to me
twelve drummers drumming
I am an inserted line.
eleven pipers piping
ten lords a leaping
nine ladies dancing
eight maids a milking
seven swans a swimming
six geese a laying
five golden rings
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree

Run

sed '/ladies dancing/i I am an inserted line before ladies dancing.' song.txt

Output

On the twelfth day of Christmas my true love gave to me
twelve drummers drumming
eleven pipers piping
ten lords a leaping
I am an inserted line before ladies dancing.
nine ladies dancing
eight maids a milking
seven swans a swimming
six geese a laying
five golden rings
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree

2.4 Append a Line

Append a Line

Run

sed '5a I am an appended line.' song.txt

Output

On the twelfth day of Christmas my true love gave to me
twelve drummers drumming
eleven pipers piping
ten lords a leaping
nine ladies dancing
eight maids a milking
seven swans a swimming
I am an appended line.
six geese a laying
five golden rings
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree

Run

sed '/ladies dancing/a I am an appended line after ladies dancing.' song.txt

Output

On the twelfth day of Christmas my true love gave to me
twelve drummers drumming
eleven pipers piping
ten lords a leaping
nine ladies dancing
I am an appended line after ladies dancing.
eight maids a milking
seven swans a swimming
six geese a laying
five golden rings
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree

2.5 Change a Line

Change a Line

Run

sed '3c I am a changed line.' song.txt

Output

On the twelfth day of Christmas my true love gave to me
twelve drummers drumming
I am a changed line.
ten lords a leaping
nine ladies dancing
eight maids a milking
seven swans a swimming
six geese a laying
five golden rings
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree

Run

sed '/ladies dancing/c I am a changed line.' song.txt

Output

On the twelfth day of Christmas my true love gave to me
twelve drummers drumming
eleven pipers piping
ten lords a leaping
I am a changed line.
eight maids a milking
seven swans a swimming
six geese a laying
five golden rings
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree

2.6 Some other commands

Print

Run

-n will enter quiet mode, and will suppress the normal stdout.

sed -n 8p song.txt

Output

six geese a laying

Run

-n will enter quiet mode, and will suppress the normal stdout.

sed -n '/golden rings/p' song.txt

Output

five golden rings

Skip Lines

Run

-n will enter quiet mode, and will suppress the normal stdout. n in the action part means switching to next line.

sed -n 'n; n; p' song.txt

Output

eleven pipers piping
eight maids a milking
five golden rings
two turtle doves

  1. For more detailed usage of sed, I suggest to visit https://www.gnu.org/software/sed/manual/sed.html