Introduction
grep is a command commonly used for string searching, but it is also frequently used in combination with replacement.
On the other hand, since grep alone cannot perform replacement, it is important to use it together with commands such as sed, find, and xargs.
This article explains everything in detail, from the basics to more advanced applications.
Reference: GNU grep
Basic syntax for grep replace
Creating a file
cat << 'EOF' > input.txt
apple
banana
apple pie
grape
pineapple
EOF
Command to run
grep "apple" input.txt
Result
apple
apple pie
pineapple
Command to run
grep "apple" input.txt | sed 's/apple/orange/g'
Result
orange
orange pie
pineorange
How it works
| Item | Description |
|---|---|
| Command | grep |
| Role | Searches for and displays lines that match a pattern |
| Can it replace? | No (grep alone cannot replace) |
| How to replace | Use it together with a replacement command such as sed |
Explanation
grep is a search-only command and has no string replacement feature.
So when people refer to "grep replace," they generally mean passing the results found by grep to sed for replacement.
How to bulk replace a specific string with grep replace
Creating a file
cat << 'EOF' > input.txt
apple
banana
apple pie
orange
apple juice
EOF
Command to run
grep -rl "apple" . | xargs sed -i 's/apple/orange/g'
Result
no output
Command to run
cat input.txt
Result
orange
banana
orange pie
orange
orange juice
How it works
| Element | Description |
|---|---|
| grep -rl "apple" . | Recursively searches for file names containing apple |
| xargs | Passes the resulting file names to sed |
| sed -i | Edits the file directly (in-place editing) |
| s/apple/orange/g | Replaces every instance of apple with orange |
Explanation
grep itself has no replacement feature, but combining it with sed lets you efficiently bulk-replace only the files that were found. This combination is commonly used for grep replace.
How to use regular expressions with grep replace
Creating a file
cat << 'EOF' > input.txt
apple
banana
grape
pineapple
Apple
EOF
Command to run
grep -E 'apple|grape' input.txt
Result
apple
grape
pineapple
Command to run
grep -E '^apple$|^grape$' input.txt
Result
apple
grape
How it works
| Item | Description |
|---|---|
| grep | A command that searches a file for lines matching a condition |
| -E | Option that enables extended regular expressions (ERE) |
| apple|grape | Matches apple or grape (OR condition) |
| ^ | Anchor representing the start of a line |
| $ | Anchor representing the end of a line |
| ^apple$ | Matches only when the entire line is exactly apple |
Explanation
grep has no replacement feature, but it allows flexible string searches using regular expressions.
When you need to perform replacement, it's common to combine it with a command such as sed or perl.
Replacing with capture groups in grep replace
Creating a file
cat << 'EOF' > input.txt
name: taro
name: hanako
name: jiro
EOF
Command to run
grep '^name:' input.txt | sed -E 's/^name: (.*)$/user=\1/'
Result
user=taro
user=hanako
user=jiro
How it works
| Item | Description |
|---|---|
| grep '^name:' | Extracts only lines starting with name: |
| sed -E | Enables extended regular expressions |
| (.*) | Captures the text after name: as a capture group |
| \1 | Expands the first capture group into the replacement text |
| s/search/replace/ | Replaces the text matched by the regular expression |
Explanation
Here, grep extracts the target lines, and sed's capture group ( ) and \1 are used to reuse the captured value while performing the replacement.
Since grep alone cannot replace, it is generally combined with a replacement command such as sed.
How to replace only matching lines with grep replace
Creating a file
cat << 'EOF' > input.txt
apple
banana
apple pie
orange
apple juice
EOF
Command to run
grep 'apple' input.txt | sed 's/apple/APPLE/'
Result
APPLE
APPLE pie
APPLE juice
Command to run
sed '/apple/s/apple/APPLE/' input.txt
Result
APPLE
banana
APPLE pie
orange
APPLE juice
How it works
| Command | How it works | Characteristics |
|---|---|---|
| grep 'apple' input.txt | sed 's/apple/APPLE/' | grep extracts only the matching lines, then sed replaces the piped output | Only matching lines are output |
| sed '/apple/s/apple/APPLE/' input.txt | sed performs the replacement only on lines containing apple | Replaces only the matching lines while keeping all the original lines |
Explanation
If you only want to replace the lines extracted by grep, pipe them to sed.
If you want to keep the original file structure while replacing only the matching lines, using sed's address specification (/pattern/) is convenient.
How to bulk replace across multiple files with grep replace
Creating a file
mkdir -p sample
Creating a file
cat << 'EOF' > sample/file1.txt
grep is useful
Search with grep
EOF
Creating a file
cat << 'EOF' > sample/file2.txt
Learn grep
grep is a search command
EOF
Command to run
grep -rl "grep" sample | xargs sed -i 's/grep/rg/g'
Result
no output
Command to run
grep -R "rg" sample
Result
sample/file2.txt:Learn rg
sample/file2.txt:rg is a search command
sample/file1.txt:rg is useful
sample/file1.txt:Search with rg
How it works
| Item | Description |
|---|---|
| grep -rl "grep" sample | Recursively retrieves only file names containing "grep" |
| xargs | Passes the retrieved file names to the following command |
| sed -i | Edits files directly and replaces the string |
| s/grep/rg/g | Replaces every "grep" on each line with "rg" |
Explanation
grep extracts the target files, and sed performs the replacement, allowing you to bulk-update multiple files.
Because the target files can be narrowed down, this lets you replace strings safely and efficiently.
How to target only a specific file extension with grep replace
Creating a file
mkdir -p src docs
Creating a file
cat << 'EOF' > src/main.py
grep foo
EOF
Creating a file
cat << 'EOF' > src/app.py
grep bar
EOF
Creating a file
cat << 'EOF' > src/index.js
grep baz
EOF
Creating a file
cat << 'EOF' > src/util.py
grep hoge
EOF
Creating a file
cat << 'EOF' > docs/readme.md
grep fuga
EOF
Command to run
grep -r 'grep' src/*.py docs/*.py | sed 's/grep/replace/g'
Result
grep: docs/*.py: No such file or directory
src/app.py:replace bar
src/main.py:replace foo
src/util.py:replace hoge
How it works
| Item | Description |
|---|---|
| mkdir -p src docs | Creates the src and docs directories (no error if they already exist) |
| src/*.py docs/*.py | Uses shell globbing to target only .py files |
| grep -r 'grep' | Searches the specified .py files for lines containing grep |
| sed 's/grep/replace/g' | Replaces grep with replace on the extracted lines |
| Scope restriction | src/index.js and docs/readme.md don't match *.py, so they are excluded from the search and replacement |
Explanation
Using the *.py glob pattern lets you limit the search target to only Python files.
This means you can safely perform the replacement without worrying about files with other extensions.
How to specify exclusion conditions with grep replace
Creating a file
cat << 'EOF' > input.txt
apple
banana
orange
grape
apple pie
banana split
orange juice
grape soda
EOF
Command to run
grep -v "apple" input.txt | sed 's/banana/BANANA/g'
Result
BANANA
orange
grape
BANANA split
orange juice
grape soda
How it works
| Item | Description |
|---|---|
| grep -v "apple" | Excludes lines containing apple (specifies the exclusion condition) |
| sed 's/banana/BANANA/g' | Replaces banana with BANANA in only the remaining lines |
| Pipe | | Passes grep's output to sed for continuous processing |
Explanation
Since grep itself has no string replacement feature, exclusion conditions are specified with grep -v while the replacement is done with sed.
This allows you to safely replace strings after excluding specific lines.
How to specify multiple conditions (AND / OR) with grep replace
Creating a file
cat << 'EOF' > input.txt
apple red
apple green
banana yellow
banana green
orange orange
grape green
apple yellow
EOF
Command to run
grep -E 'apple|banana' input.txt | sed 's/apple/APPLE/g; s/banana/BANANA/g'
Result
APPLE red
APPLE green
BANANA yellow
BANANA green
APPLE yellow
Command to run
grep 'apple' input.txt | grep 'green' | sed 's/apple/APPLE/g; s/green/GREEN/g'
Result
APPLE GREEN
Command to run
grep 'apple' input.txt | grep 'yellow' | sed 's/apple/APPLE/g; s/yellow/YELLOW/g'
Result
APPLE YELLOW
Command to run
grep -E 'apple|banana' input.txt | grep 'green' | sed 's/apple/APPLE/g; s/banana/BANANA/g; s/green/GREEN/g'
Result
APPLE GREEN
BANANA GREEN
Command to run
sed -i -e 's/apple/APPLE/g' -e 's/banana/BANANA/g' input.txt
Result
no output
Command to run
cat input.txt
Result
APPLE red
APPLE green
BANANA yellow
BANANA green
orange orange
grape green
APPLE yellow
How it works
| Condition | Example command | How it works |
|---|---|---|
| OR search + replace | grep -E 'apple|banana' input.txt | sed 's/apple/APPLE/g; s/banana/BANANA/g' | Uses grep's -E option to perform an OR search with a regular expression, then replaces the matching lines with sed. |
| AND search + replace | grep 'apple' input.txt | grep 'green' | sed 's/apple/APPLE/g' | Chains multiple grep commands with a pipe, and only the lines matching all conditions are changed by sed. |
| OR + AND search + replace | grep -E 'apple|banana' input.txt | grep 'green' | sed 's/apple/APPLE/g; s/banana/BANANA/g; s/green/GREEN/g' | Narrows down the target with an OR condition, then further narrows it with an AND condition before replacing with sed. |
| Direct file replacement | sed -i -e 's/old/new/g' file | With the -i option, the changes are written to the original file instead of standard output. |
Explanation
Since grep is a search-only command, it has no replacement feature.
You can achieve "grep replace" by building the search condition with grep and passing the replacement processing to sed.
How to combine grep replace with the find command
Creating a file
mkdir -p src docs
Creating a file
cat << 'EOF' > src/main.txt
This is a grep replacement sample.
EOF
Creating a file
cat << 'EOF' > src/test.txt
Run grep replacement.
EOF
Creating a file
cat << 'EOF' > docs/readme.md
Explanation of grep replacement.
EOF
Command to run
find . \( -path "./src/*" -o -path "./docs/*" \) -type f -exec grep -H "replacement" {} \;
Result
./docs/readme.md:Explanation of grep replacement.
./src/main.txt:This is a grep replacement sample.
./src/test.txt:Run grep replacement.
Command to run
find . \( -path "./src/*" -o -path "./docs/*" \) -type f -exec sed -i 's/replacement/search and replace/g' {} \;
Result
no output
Command to run
find . \( -path "./src/*" -o -path "./docs/*" \) -type f -exec grep -H "search and replace" {} \;
Result
./docs/readme.md:Explanation of grep search and replace.
./src/main.txt:This is a grep search and replace sample.
./src/test.txt:Run grep search and replace.
How it works
| Element | Description |
|---|---|
| find . | Searches recursively under the current directory |
| -path "./src/*" -o -path "./docs/*" | Targets only files under src or docs |
| -type f | Targets only files |
| -exec grep "replacement" {} \; | Searches each found file for the string |
| -exec sed -i 's/replacement/search and replace/g' {} \; | Bulk-replaces the string in each found file |
| {} | Holds the file name found by find |
| \; | Marks the end of -exec |
Explanation
By narrowing down target files with find, searching with grep, and replacing with sed, you can efficiently process multiple files.
Using -exec lets you run any command against each file that was found.
How to bulk replace using xargs with grep replace
Creating a file
cat << 'EOF' > input.txt
apple
orange
apple
grape
apple
EOF
Command to run
grep -rl "apple" . --include="*.txt" | xargs sed -i 's/apple/banana/g'
Result
no output
Command to run
cat input.txt
Result
banana
orange
banana
grape
banana
How it works
| Element | Role |
|---|---|
| grep -rl "apple" . | Recursively searches for only file names containing apple |
| --include="*.txt" | Limits the target to only .txt files |
| xargs | Passes the list of files retrieved by grep to the following command |
| sed -i 's/apple/banana/g' | Bulk-replaces apple with banana inside each file |
| cat input.txt | Checks the content after replacement |
Explanation
By using grep to extract only the target files and passing that list to sed via xargs, you can perform bulk replacement efficiently.
Even in an environment with a large number of files, you avoid processing unnecessary files, so it runs fast.
How to preview replacement results with grep replace (dry run)
Creating a file
cat << 'EOF' > input.txt
apple
banana
apple pie
grape
pineapple
EOF
Command to run
grep 'apple' input.txt
Result
apple
apple pie
pineapple
Command to run
grep 'apple' input.txt | sed 's/apple/orange/g'
Result
orange
orange pie
pineorange
Command to run
sed 's/apple/orange/g' input.txt
Result
orange
banana
orange pie
grape
pineorange
Command to run
cat input.txt
Result
apple
banana
apple pie
grape
pineapple
How it works
| Command | Role | File change |
|---|---|---|
| grep 'apple' input.txt | Extracts only the lines to be replaced | None |
| grep 'apple' input.txt | sed 's/apple/orange/g' | Previews the replacement result for the extracted lines only | None |
| sed 's/apple/orange/g' input.txt | Displays the replacement result for the entire file to standard output | None |
| cat input.txt | Confirms that the original file has not been changed | None |
Explanation
Since grep lets you check the target lines beforehand, you can use it as a dry run before actually performing the replacement.
Combined with sed, you can safely check the results of "grep replace" without modifying the file.
Causes and fixes when grep replace doesn’t work
Creating a file
cat << 'EOF' > input.txt
apple
banana
grape
EOF
Command to run
grep "apple" input.txt | sed 's/apple/orange/'
Result
orange
Command to run
grep "orange" input.txt | sed 's/orange/apple/'
Result
no output
Command to run
sed -i 's/apple/orange/' input.txt
Result
no output
Command to run
cat input.txt
Result
orange
banana
grape
How it works
| Item | Description |
|---|---|
| Cause | grep is a search-only command and has no feature to rewrite files (no replacement feature). |
| Why it can't replace | grep only prints matching lines to standard output; it does not change the input file. |
| Fix | Replace the standard output with sed 's/search string/replace string/'. |
| How to replace the file directly | Use sed -i 's/search string/replace string/' filename. |
| Role of grep | Searches for and extracts the target string. |
| Role of sed | Performs string replacement/editing. |
Explanation
Since grep is a search-only command, it cannot replace strings on its own.
If you want to change a string, you need to combine it with a text-editing command such as sed or perl.
How to automate grep replace with a shell script
Creating a file
cat << 'EOF' > input.txt
apple
banana
apple
orange
EOF
Creating a file
cat << 'EOF' > replace.sh
#!/bin/bash
# Search with grep and replace with sed
TARGET="apple"
REPLACE="grape"
grep -n "$TARGET" input.txt
sed -i "s/$TARGET/$REPLACE/g" input.txt
echo "After replacement:"
cat input.txt
EOF
Command to run
chmod +x replace.sh
Result
no output
Command to run
./replace.sh
Result
1:apple
3:apple
After replacement:
grape
banana
grape
orange
How it works
| Process | Command used | Description |
|---|---|---|
| Search for the target string | grep | Checks the file for the string to be replaced |
| Replacement | sed | Changes the specified string to another string |
| Automation | Shell script | Runs the grep search and sed replacement as a single sequence |
| Granting execute permission | chmod | Makes the script directly executable |
Explanation
By checking the target string with grep and turning the sed replacement into a shell script, you can automate string replacement work across a large number of files.
Using this approach lets you repeat the same replacement process, reducing work time and preventing input mistakes.
Key points on grep replace
grep replace is an efficient way of working that combines searching and replacing.
Once you understand the basic syntax, learning regular expressions, multiple conditions, and how to combine grep with find and xargs will let you edit even large numbers of files quickly.
By learning step by step from the basics, even beginners can acquire practical command-line skills.

