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' fileCommon 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 placeHow 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. Sosed 's/.../.../' fileis safe to preview before you add-i. The-iflag rewrites the file in place, and a mistake there is irreversible: run it without-ifirst, or use-i.bakto keep a backup copy. The pattern ins///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.