Skip to content

SEARCH AND REPLACE - Finding and replacing text in files

Python
#!/usr/bin/env python3
"""
SEARCH AND REPLACE - Finding and replacing text in files
Demonstrates pattern matching and text replacement
"""

import os
import tempfile
import re

print("=" * 60)
print("SEARCH AND REPLACE - Text Modification")
print("=" * 60)

temp_dir = tempfile.gettempdir()

# Create sample file
source_file = os.path.join(temp_dir, "document.txt")
with open(source_file, 'w') as f:
    f.write("""The quick brown fox jumps over the lazy dog.
The quick brown fox is very quick.
A lazy dog sleeps all day.
The fox and the dog are friends.
Quick brown foxes are clever animals.
""")

print("Original document:")
with open(source_file, 'r') as f:
    print(f.read())

# Example 1: Simple string replacement
print("\n1. Simple String Replacement")
print("-" * 40)
output1 = os.path.join(temp_dir, "replaced1.txt")

with open(source_file, 'r') as src, open(output1, 'w') as dst:
    for line in src:
        modified = line.replace('fox', 'cat')
        dst.write(modified)

print("Replaced 'fox' with 'cat':")
with open(output1, 'r') as f:
    print(f.read())

# Example 2: Case-insensitive replacement
print("\n2. Case-Insensitive Replacement")
print("-" * 40)
output2 = os.path.join(temp_dir, "replaced2.txt")

with open(source_file, 'r') as src, open(output2, 'w') as dst:
    for line in src:
        # Use regex for case-insensitive
        modified = re.sub(r'quick', 'fast', line, flags=re.IGNORECASE)
        dst.write(modified)

print("Replaced 'quick'/'Quick' with 'fast':")
with open(output2, 'r') as f:
    print(f.read())

# Example 3: Multiple replacements
print("\n3. Multiple Replacements")
print("-" * 40)
output3 = os.path.join(temp_dir, "replaced3.txt")

replacements = {
    'fox': 'wolf',
    'dog': 'cat',
    'lazy': 'energetic'
}

with open(source_file, 'r') as src, open(output3, 'w') as dst:
    for line in src:
        modified = line
        for old, new in replacements.items():
            modified = modified.replace(old, new)
        dst.write(modified)

print("Multiple replacements:")
with open(output3, 'r') as f:
    print(f.read())

# Example 4: Counting replacements
print("\n4. Counting Replacements")
print("-" * 40)
output4 = os.path.join(temp_dir, "replaced4.txt")

total_replacements = 0
with open(source_file, 'r') as src, open(output4, 'w') as dst:
    for line_num, line in enumerate(src, 1):
        count = line.count('the')
        if count > 0:
            print(f"  Line {line_num}: {count} occurrence(s)")
            total_replacements += count
        modified = line.replace('the', 'a')
        dst.write(modified)

print(f"Total replacements: {total_replacements}")

# Example 5: Word boundary replacement (regex)
print("\n5. Word Boundary Replacement")
print("-" * 40)
output5 = os.path.join(temp_dir, "replaced5.txt")

with open(source_file, 'r') as src, open(output5, 'w') as dst:
    for line in src:
        # Only replace 'fox' as complete word, not 'foxes'
        modified = re.sub(r'\bfox\b', 'wolf', line)
        dst.write(modified)

print("Replace 'fox' but not 'foxes':")
with open(output5, 'r') as f:
    print(f.read())

# Example 6: In-place replacement (read all, write back)
print("\n6. In-Place Replacement")
print("-" * 40)
temp_file = os.path.join(temp_dir, "inplace.txt")

# Create file
with open(temp_file, 'w') as f:
    f.write("Version 1.0.0\nRelease date: 2024-01-01\n")

print("Before:")
with open(temp_file, 'r') as f:
    print(f.read())

# Read all content
with open(temp_file, 'r') as f:
    content = f.read()

# Modify
content = content.replace('1.0.0', '2.0.0')
content = content.replace('2024-01-01', '2024-06-01')

# Write back
with open(temp_file, 'w') as f:
    f.write(content)

print("After:")
with open(temp_file, 'r') as f:
    print(f.read())

# Example 7: Conditional replacement
print("\n7. Conditional Replacement")
print("-" * 40)
output7 = os.path.join(temp_dir, "replaced7.txt")

with open(source_file, 'r') as src, open(output7, 'w') as dst:
    for line in src:
        # Only replace in lines that contain 'brown'
        if 'brown' in line:
            modified = line.replace('fox', 'CAT')
            dst.write(f"[MODIFIED] {modified}")
        else:
            dst.write(line)

print("Replace 'fox' only in lines with 'brown':")
with open(output7, 'r') as f:
    print(f.read())

# Example 8: Pattern-based replacement with regex
print("\n8. Pattern-Based Replacement (Regex)")
print("-" * 40)
numbers_file = os.path.join(temp_dir, "numbers.txt")

with open(numbers_file, 'w') as f:
    f.write("Price: $10.50\n")
    f.write("Cost: $25.99\n")
    f.write("Total: $150.00\n")

output8 = os.path.join(temp_dir, "replaced8.txt")

with open(numbers_file, 'r') as src, open(output8, 'w') as dst:
    for line in src:
        # Replace dollar amounts with euros (simplified)
        modified = re.sub(r'\$(\d+\.\d{2})', r'€\1', line)
        dst.write(modified)

print("Replace $ with €:")
with open(output8, 'r') as f:
    print(f.read())

# Example 9: Remove lines matching pattern
print("\n9. Remove Lines Matching Pattern")
print("-" * 40)
output9 = os.path.join(temp_dir, "filtered.txt")

with open(source_file, 'r') as src, open(output9, 'w') as dst:
    for line in src:
        # Skip lines containing 'lazy'
        if 'lazy' not in line:
            dst.write(line)

print("Removed lines containing 'lazy':")
with open(output9, 'r') as f:
    print(f.read())

# Example 10: Search and highlight (not replace)
print("\n10. Search and Highlight Matches")
print("-" * 40)
output10 = os.path.join(temp_dir, "highlighted.txt")

with open(source_file, 'r') as src, open(output10, 'w') as dst:
    for line_num, line in enumerate(src, 1):
        if 'fox' in line:
            # Add markers around matches
            highlighted = line.replace('fox', '>>FOX<<')
            dst.write(f"Line {line_num}: {highlighted}")

print("Highlighted 'fox' occurrences:")
with open(output10, 'r') as f:
    print(f.read())

# Cleanup
for f in [source_file, output1, output2, output3, output4, output5,
          temp_file, output7, numbers_file, output8, output9, output10]:
    if os.path.exists(f):
        os.remove(f)

print("\n" + "=" * 60)
print("Key Points:")
print("  - str.replace() for simple replacements")
print("  - re.sub() for pattern matching")
print("  - Read line by line, write modified")
print("  - Can count, filter, or conditionally replace")
print("  - For in-place: read all, modify, write back")
print("=" * 60)