Linux for Developers Course
Linux for Developers
/
Advanced

sed

Definition

Stream Editor. A powerful utility that parses and transforms text. It is most commonly used for finding and replacing text across massive files or data streams without opening them in an editor.

Explain Like I'm New

The ultimate 'Find and Replace' tool. You can tell it 'Go into this 10,000 line file, find every instance of the word Apple, and change it to Orange instantly'.

Real World Example

You are deploying code and need to change the database IP from '127.0.0.1' to '192.168.1.5' inside a config file. You script it: `sed -i 's/127.0.0.1/192.168.1.5/g' config.txt`. The file is updated instantly.

Common Use Cases

  • •Automated text replacement
  • •Data sanitization
  • •CI/CD scripting

Crucial Command Flags

Flag / OptionDescription
-iIn-place. Edit files directly instead of outputting to terminal.
-eExpression. Add the script to the commands to be executed.
-nQuiet. Suppress automatic printing of pattern space.

Interview Questions

basic

  • In the `sed` command `s/old/new/g`, what does the 's' stand for?

intermediate

  • What does the `-i` flag do in a `sed` command?

Flash Cards

Question

What is 's'?

Click to reveal answer
Answer

Substitute.

Question

What does -i do?

Click to reveal answer
Answer

In-place editing. By default, `sed` only prints the changed text to the screen without actually modifying the file. The `-i` flag tells `sed` to permanently save the changes directly into the original file.