Forever & AlwaysLINUX COMMAND ATLAS

LINUX COMMAND ATLAS · COMMAND GUIDE

Linux sed command

Transform text streams; this example prints matching lines without editing files.

GNU sed

Syntax

Command form
sed [OPTION]... SCRIPT [FILE]...

Common options

6 options
OptionWhat it does
-n, --quietSuppress automatic printing; use p to print selected lines.
-E, -r, --regexp-extendedUse extended regular expressions.
-e SCRIPTAdd a script to the commands to be executed.
-f FILERead editing commands from FILE.
-i[SUFFIX], --in-placeEdit files in place; an optional suffix keeps a backup.
-z, --null-dataSeparate input records with NUL characters.

Usage examples

3 examples

Transform text streams; this example prints matching lines without editing files.

sed -n "/WARN/p" app.log

Print lines 1 to 20 without printing other lines.

sed -n '1,20p' app.log

Replace every old occurrence on each line in the output.

sed 's/old/new/g' names.txt

Notes

Without -i, sed writes transformed text to standard output and leaves the source file unchanged. Test scripts on sample data first.