Text and processing

The sed command

Edits text as a stream: above all, replaces words inside lines.

Syntax

sed 's/antiguo/nuevo/[g]' [archivo]

Most useful options

s/x/y/Replaces the first x of each line
s/x/y/gReplaces all
NdDeletes line N

Copy-ready examples

$ echo hola mundo | sed 's/mundo/linux/'Replace a word
$ echo aaa | sed 's/a/A/g'All a's to A
$ sed 2d Documentos/tareas.txtDeletes the second line

You can try them in the browser terminal: it does not touch your computer.

Typical mistakes

  • Without /g it only changes the first match of each line.

Related commands

FAQ

Does sed change the original file?

Not by default: it writes the result on screen. To save it, redirect to a file.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course