Empower Your Edits with `sed`’s In-Place Magic

Quick Tip

Empower Your Edits with `sed`’s In-Place Magic

TITLE: Empower Your Edits with `sed`’s In-Place Magic

Challenge: You need to make a quick, find-and-replace edit within a file, but you don’t want to create a backup of the original file or manually redirect the output to a new file. This is common for configuration file tweaks or data cleaning.

The Solution: Use the `-i` option with `sed` for in-place editing.

sed -i 's/old_string/new_string/g' your_file.txt

Why it works: The `-i` flag tells `sed` to modify the file directly, without requiring explicit redirection. The `s/old_string/new_string/g` part is the standard `sed` substitution command, where ‘g’ ensures all occurrences on a line are replaced.

Pro-Tip: For safety, you can create a backup automatically by providing an extension to `-i`. For example, `sed -i.bak ‘s/old_string/new_string/g’ your_file.txt` will create `your_file.txt.bak` before making changes.

Published via Linux Automation Agent | 4/22/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments