Contents

Mastering the Sed Command in Linux

Introduction

Sed (Stream EDitor) is a powerful text transformation tool that processes text line-by-line using a compact scripting language. It’s particularly useful for making batch edits to files and filtering text streams.

Why Learn sed?

  • Perform non-interactive text editing
  • Edit files without opening them
  • Process large files efficiently
  • Use regular expressions for pattern matching
  • Automate repetitive text transformations

Basic Syntax

The basic syntax for sed is:

sed [options] 'script' input-file

sed Cheat Sheet

OptionDescription
-nSuppress automatic printing
-iEdit files in-place
-eAdd multiple commands
-rUse extended regex syntax
-fRead script from file
CommandDescription
sSubstitute text
pPrint lines
dDelete lines
aAppend text after line
iInsert text before line
qQuit processing

Sample Files Setup

Create test files for our examples:

echo "Hello World
Welcome to Linux
The quick brown fox
jumps over the lazy dog
SED is awesome
12345
Goodbye World" > poem.txt

echo "John,25,New York
Alice,30,London
Bob,22,Paris
Carol,35,Tokyo" > data.csv

Basic Examples

Example 1: Simple Substitution (s command)

sed 's/World/UNIX/' poem.txt
HelloPlanet
WelcometoLinux
Thequickbrownfox
jumpsoverthelazydog
SEDisawesome
12345
GoodbyePlanet

Example 2: Delete Lines (d command)

sed '3,5d' poem.txt
HelloPlanet
WelcometoLinux
12345
GoodbyePlanet

Example 3: Print Specific Lines (p command)

sed -n '2,4p' poem.txt
WelcometoLinux
Thequickbrownfox
jumpsoverthelazydog

Intermediate Examples

Example 4: Multiple Commands (-e option)

sed -e 's/Linux/Unix/' -e '/dog/s/lazy/active/' poem.txt
HelloPlanet
WelcometoUnix
Thequickbrownfox
jumpsovertheactivedog
SEDisawesome
12345
GoodbyePlanet

Example 5: In-place Editing (-i option)

cp poem.txt poem-backup.txt
sed -i 's/World/Planet/' poem.txt
cat poem.txt
HelloPlanet
WelcometoLinux
Thequickbrownfox
jumpsoverthelazydog
SEDisawesome
12345
GoodbyePlanet

Example 6: Using Regular Expressions

sed '/^[A-Z]/d' poem.txt
jumpsoverthelazydog
12345

Advanced Examples

Example 7: Group Replacement (& and \1)

sed -r 's/([0-9]+)/Number: \1/' poem.txt
HelloPlanet
WelcometoLinux
Thequickbrownfox
jumpsoverthelazydog
SEDisawesome
Number:12345
GoodbyePlanet

Example 8: Append/Insert Text (a and i commands)

sed '3a\--- Add new line after line 3 ---' poem.txt
HelloPlanet
WelcometoLinux
Thequickbrownfox
Addnewlineafterline3
jumpsoverthelazydog
SEDisawesome
12345
GoodbyePlanet

Example 9: Complex Transformation (CSV to HTML)

sed -e '1i\<table>' \
    -e 's/^/<tr><td>/' \
    -e 's/,/<\/td><td>/g' \
    -e 's/$/<\/td><\/tr>/' \
    -e '$a\</table>' \
    data.csv
<table>
<tr><td>John</td><td>25</td><td>NewYork</td></tr>
<tr><td>Alice</td><td>30</td><td>London</td></tr>
<tr><td>Bob</td><td>22</td><td>Paris</td></tr>
<tr><td>Carol</td><td>35</td><td>Tokyo</td></tr>
</table>

Regular Expressions with sed

1. Address Ranges

sed '2,4s/o/O/g' poem.txt
HelloPlanet
WelcOmetOLinux
ThequickbrOwnfOx
jumpsOverthelazydOg
SEDisawesome
12345
GoodbyePlanet

2. Start/End Anchors

sed '/^SED/,/^Goodbye/d' poem.txt
HelloPlanet
WelcometoLinux
Thequickbrownfox
jumpsoverthelazydog

Pro Tips

  1. Combine with other commands:
grep 'Linux' poem.txt | sed 's/Linux/Unix/'
Welcome to Unix
  1. Use sed scripts for complex tasks:
echo "s/World/Planet/
s/Linux/Unix/" > script.sed
sed -f script.sed poem.txt
HelloPlanet
WelcometoUnix
Thequickbrownfox
jumpsoverthelazydog
SEDisawesome
12345
GoodbyePlanet
  1. Handle whitespace carefully:
sed -E 's/\s+/ /g' poem.txt  # Collapse multiple spaces
HelloPlanet
WelcometoLinux
Thequickbrownfox
jumpsoverthelazydog
SEDisawesome
12345
GoodbyePlanet