File Viewing & Editing

sed

Stream editor for filtering and transforming text using pattern-based rules.

Synopsis

syntax
sed [OPTION]... 'SCRIPT' [FILE]...

Examples

Replace all occurrences of old with new
sed 's/old/new/g' file.txt
In-place replacement across files
sed -i 's/foo/bar/g' *.txt
Print only lines 10 through 20
sed -n '10,20p' file.txt
Delete comment lines
sed '/^#/d' config.txt
Replace all numbers with NUM
sed -E 's/[0-9]+/NUM/g' file.txt

Common options

FlagDescription
-iEdit files in place
-nSuppress automatic output
-eAdd script expression
-E, -rUse extended regex
-fRead script from file

About sed

The `sed` command stream editor for filtering and transforming text using pattern-based rules. Text viewing and editing commands are fundamental tools in any Linux user's toolkit.

Linux treats almost everything as a file, so the ability to quickly inspect, filter, transform, and edit file contents from the command line is critical. These commands are regularly combined with pipes and redirects to build powerful data-processing pipelines.

The command accepts 5 commonly used flags shown above, though the full set of options is available in the man page (`man sed`). The 5 examples on this page cover typical real-world usage patterns that you can copy and adapt for your own workflows.

Related commands

More File Viewing & Editing Commands

Other commands in the File Viewing & Editing category

Related tools