Text processing

Linux sed command

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

Availability: Common Linux commands; shell built-ins and external implementations may differ by distribution.中文

Syntax

sed [OPTION]... SCRIPT [FILE]...

Examples

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

Parameters, options and clauses

Option / clauseDescription
-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.
Safety note

Review a command and its target before running it. Commands that alter files, services, accounts, disks, permissions or databases may be difficult to reverse. Test in a safe environment and keep backups.

Official reference ↗Back to directory