Bash Script to Find and Replace Text in a File
Use
sed -i 's/old_text/new_text/g' filename in a Bash script to find and replace all occurrences of old_text with new_text directly in the file.Examples
InputFile content: Hello world
Command: sed -i 's/world/universe/g' file.txt
OutputFile content after: Hello universe
InputFile content: apple, banana, apple
Command: sed -i 's/apple/orange/g' fruits.txt
OutputFile content after: orange, banana, orange
InputFile content: no match here
Command: sed -i 's/xyz/abc/g' file.txt
OutputFile content after: no match here (unchanged)
How to Think About It
To replace text in a file using Bash, think about searching for the exact text you want to change and then replacing it everywhere it appears. The
sed command is perfect because it can find and replace text directly inside the file without needing extra steps.Algorithm
1
Get the filename, the text to find, and the text to replace from the user or script arguments.2
Use a command that searches the file for the target text.3
Replace all occurrences of the target text with the new text.4
Save the changes directly to the original file.Code
bash
#!/bin/bash # Variables for old and new text and filename old_text="apple" new_text="orange" file="fruits.txt" # Replace all occurrences of old_text with new_text in the file sed -i "s/${old_text}/${new_text}/g" "$file" # Print the updated file content cat "$file"
Output
orange, banana, orange
Dry Run
Let's trace replacing 'apple' with 'orange' in 'fruits.txt' containing 'apple, banana, apple'.
1
Initial file content
apple, banana, apple
2
Run sed command
sed -i 's/apple/orange/g' fruits.txt
3
File content after replacement
orange, banana, orange
| Step | Action | File Content |
|---|---|---|
| 1 | Start | apple, banana, apple |
| 2 | Replace 'apple' with 'orange' | orange, banana, orange |
Why This Works
Step 1: Using sed for replacement
The sed command edits text in a file by applying a substitution pattern.
Step 2: The -i option
The -i flag tells sed to edit the file in place, saving changes directly.
Step 3: The substitution pattern
The pattern s/old/new/g means replace all (g) occurrences of old with new.
Alternative Approaches
Using perl command
bash
perl -pi -e 's/old_text/new_text/g' filenamePerl can also do in-place replacement and supports more complex patterns but may not be installed by default.
Using temporary file with grep and mv
bash
grep -rl 'old_text' . | xargs sed 's/old_text/new_text/g' > temp && mv temp filename
This method uses a temporary file and is less efficient but works if <code>sed -i</code> is not available.
Complexity: O(n) time, O(1) space
Time Complexity
The script reads through the file once, so time grows linearly with file size.
Space Complexity
The replacement is done in place, so no extra space proportional to file size is needed.
Which Approach is Fastest?
Using sed -i is fastest and simplest for plain text replacements compared to alternatives like Perl or temporary files.
| Approach | Time | Space | Best For |
|---|---|---|---|
| sed -i | O(n) | O(1) | Simple in-place text replacement |
| perl -pi | O(n) | O(1) | Complex patterns, regex support |
| temp file with sed | O(n) | O(n) | Systems without sed -i support |
Always backup your file before running in-place replacements to avoid accidental data loss.
Forgetting to use the
-i option causes sed to print changes to the screen without saving them.