Linuxlab

sed command in Linux: stream text editing

Updated July 3, 2026

The sed command edits text line by line on the fly, most often substituting by pattern.

Syntax

sed 's/pattern/replacement/flags' file

Common cases

sed 's/DEBUG/INFO/' log      # replace the first match on a line
sed 's/DEBUG/INFO/g' log     # g: every match on the line
sed -n '1,10p' file          # print lines 1 to 10
sed -i 's/foo/bar/g' file    # -i: edit the file in place

How it works: a stream, not an editor

sed (stream editor) reads line by line, applies its commands, and writes the result to standard output - the file itself is untouched. So sed 's/.../.../' file is safe to preview before you add -i. The -i flag rewrites the file in place, and a mistake there is irreversible: run it without -i first, or use -i.bak to keep a backup copy. The pattern in s/// is a regular expression.

Used in

You need this in the lesson Text: grep, sed, awk. The theory is the chapter Bash in depth.

See also

The grep command, The awk command, Regular expressions.

Try it

Open the sandbox: a real throwaway Linux terminal in your browser. Run the commands above by hand - the container is deleted when you leave.