Search and replace
Text editing
File manipulation
Text processing
Find and replace

How to search and replace text in a file

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Search and replace looks simple, but careless edits can break code, configuration, or data files quickly. A safe workflow includes preview, scoped matching, backups, and verification after changes. The right tool depends on whether you need a quick one-file edit, a scripted transformation, or regex-based rewriting across many files.

One-File Replacement with sed

For quick Unix-like shell edits, sed is fast and available almost everywhere.

Preview without modifying file:

bash
sed 's/old_value/new_value/g' app.conf

Edit in place with backup:

bash
sed -i.bak 's/old_value/new_value/g' app.conf

Keeping .bak backups is useful when you are applying risky replacements under time pressure.

Scripted Replacement in Python

Python is better when you need encoding control, conditionals, and testable behavior.

python
1from pathlib import Path
2
3
4def replace_text(path: Path, old: str, new: str) -> bool:
5    original = path.read_text(encoding="utf-8")
6    updated = original.replace(old, new)
7
8    if updated == original:
9        return False
10
11    path.write_text(updated, encoding="utf-8")
12    return True
13
14changed = replace_text(Path("app.conf"), "old_value", "new_value")
15print("changed" if changed else "no changes")

This approach is explicit and easy to run in CI.

Regex Replacements for Structured Patterns

When the target varies, use regex carefully.

python
1import re
2from pathlib import Path
3
4path = Path("settings.ini")
5text = path.read_text(encoding="utf-8")
6
7updated = re.sub(r"^port\s*=\s*\d+$", "port = 8080", text, flags=re.MULTILINE)
8path.write_text(updated, encoding="utf-8")

Anchor patterns tightly. Broad regex often overmatches and changes unrelated lines.

Multi-File Workflow with Preview

For repository-wide replacement, list candidate files first, then apply controlled edits.

bash
rg --files | rg '\.py$'

Then run a scripted replacement only on that set. Avoid touching generated directories, build output, and lock files unless intentionally targeted.

Idempotent Replacement Scripts

A good replacement script is idempotent, meaning re-running it does not introduce new changes after first success. This makes reruns safe in CI and reduces merge conflicts during coordinated refactors.

python
1from pathlib import Path
2
3
4def replace_in_many(paths, old, new):
5    changed = []
6    for p in paths:
7        text = p.read_text(encoding="utf-8")
8        updated = text.replace(old, new)
9        if updated != text:
10            p.write_text(updated, encoding="utf-8")
11            changed.append(str(p))
12    return sorted(changed)

Print changed paths in sorted order so review output is deterministic.

Validation After Replacement

Never treat replacement as complete until validated.

bash
git diff --stat
git diff
pytest -q

For config files, add environment-specific validation commands such as parser checks or service startup tests.

Encoding and Line Ending Concerns

Text replacements can unintentionally alter file encoding or line endings. If a repository mixes encodings or platform conventions, preserve original settings where possible. In Python, be explicit about encoding and avoid opening files in binary-unaware editor tools during scripted changes.

On cross-platform teams, verify that replacements did not create noisy line-ending diffs.

Rollback-Friendly Replacement Workflow

For high-risk edits, run replacements in a temporary branch and checkpoint frequently. A practical sequence is:

  1. Create a dedicated branch.
  2. Run replacement script in check mode.
  3. Apply write mode.
  4. Inspect diff and run tests.
  5. Commit only intended files.

This keeps rollback simple and prevents emergency fixes from mixing with unrelated changes.

Common Pitfalls

  • Running in-place replacement without backup or version-control checkpoint.
  • Using regex patterns that are too broad for the intended scope.
  • Applying repository-wide replacement without exclusion rules.
  • Ignoring file encoding and corrupting non-UTF text.
  • Skipping post-change tests and committing broken results.

Summary

  • Use sed for fast one-file changes and Python for safer scripted workflows.
  • Preview and scope replacements before writing changes.
  • Keep regex patterns narrow and test-driven.
  • Build idempotent replacement scripts for repeatable refactors.
  • Validate with diffs and tests before merging.

Course illustration
Course illustration

All Rights Reserved.