Challenge

Problem

Batch text processing is essential for refactoring, configuration updates, and code migrations. This drill teaches you to search multiple files for patterns, perform replacements, handle different file encodings, and create backup copies. You'll learn glob patterns for file matching, regular expressions, in-place file editing, and safe file operationsโ€”essential skills for building developer tools, automation scripts, and migration utilities.

Difficulty: Intermediate

Instructions

  1. Read configuration from replace.txt:
    • Line 1: search pattern (can be regex)
    • Line 2: replacement text
    • Line 3: file pattern (e.g., '.txt' or '.rb')
  2. Find all files matching the pattern in current directory and subdirectories
  3. For each file:
    • Search for pattern
    • If found, create backup with .bak extension
    • Replace all occurrences
    • Count replacements
  4. Print report:
    'FIND AND REPLACE REPORT'
    '=' * 50
    'Search: [pattern]'
    'Replace: [replacement]'
    'Files: [pattern]'
    '-' * 50
    'Modified: [filename] (X replacements)'
    (for each modified file)
    '-' * 50
    'SUMMARY'
    'Files scanned: X'
    'Files modified: Y'
    'Total replacements: Z'

Files

Editable
Read-only

Hints

Hint 1

File.readlines(file) returns array of lines with newlines

Hint 2

Use .map(&:strip) to remove whitespace from each line

Hint 3

Dir.glob('**/' + pattern) searches recursively

Hint 4

File.file?(path) checks if path is a regular file (not directory)

Hint 5

string.scan(pattern).length counts occurrences

Hint 6

string.gsub(pattern, replacement) replaces all occurrences

Hint 7

Create backup: File.write(path + '.bak', content)

Hint 8

Sort files alphabetically: array.sort_by { |item| item[:path] }

Provided Files (Read-only)

1. Replace TODO with FIXME in text files

Input:
find_and_replace('replace.txt')
Expected Output:
FIND AND REPLACE REPORT
==================================================
Search: TODO
Replace: FIXME
Files: *.txt
--------------------------------------------------
Modified: file1.txt (2 replacements)
Modified: subfolder/file3.txt (2 replacements)
--------------------------------------------------
SUMMARY
Files scanned: 3
Files modified: 2
Total replacements: 4

2. Replace in Ruby files

Input:
find_and_replace('replace.txt')
Expected Output:
FIND AND REPLACE REPORT
==================================================
Search: old_method
Replace: new_method
Files: *.rb
--------------------------------------------------
Modified: lib/helper.rb (1 replacements)
Modified: script1.rb (1 replacements)
Modified: script2.rb (2 replacements)
--------------------------------------------------
SUMMARY
Files scanned: 3
Files modified: 3
Total replacements: 4

3. No matches found

Input:
find_and_replace('replace.txt')
Expected Output:
FIND AND REPLACE REPORT
==================================================
Search: NOTFOUND
Replace: REPLACEMENT
Files: *.txt
--------------------------------------------------
(No files modified)
--------------------------------------------------
SUMMARY
Files scanned: 2
Files modified: 0
Total replacements: 0
+ 2 hidden test cases