Viewing and processing text

Command sed

Edit text flow according to rules

Linux · Bash examples · commands run on the selected server

When to use sed

Use sed for stream transformations and selected-line output. Preview the resulting text before replacing configuration files, and keep the input and output paths distinct when using shell redirection.

Syntax

sed [OPTIONS] 'SCRIPT' [FILE...]

Square brackets mark optional arguments; an ellipsis means repetition. Replace example file names, user names and server addresses with your own values.

Common options and arguments

-n
Don't print every line automatically.
-E
Use extended regular expressions.
-i.bak
Edit in place and keep a .bak backup with GNU sed.

Usage examples

Replace text in output

sed 's/staging/production/g' ./app.conf

Every occurrence of the selected text on each line is replaced in the output only.

Show row range

sed -n '10,20p' ./app.log

Only the requested line range is printed because automatic output is disabled.

Save modified copy

sed 's/debug=true/debug=false/' ./app.conf > ./app.new.conf

The transformed configuration is written to a new file for review rather than modifying the original.

Notes and limitations

  • A normal call prints the result and does not change the source; -i changes the file.
  • The syntax of -i and regular expressions differs between GNU and BSD sed; first check the result without recording.

Common questions and troubleshooting

Why did redirection erase my input file?

The shell opens an output redirection before sed reads input. Never redirect a transformation back into its own input path.

Does this edit the original configuration?

The shown commands produce output streams. In-place editing is a separate mode; review a new file and keep a backup before replacing production configuration.

Further help

Options vary by version and distribution. Check the installed documentation with this command: man sed.