Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to grep and replace a word in a file on Linux?
While there are plenty of ways to print and make use of specific words from a particular file in a Linux directory, when we talk about grepping a particular word and then replacing it with some other word, we need to use specific Linux utility commands.
The two Linux utility commands that we must be aware of are −
find − used to locate a particular file or directory
sed − short for stream editor and is used to perform functions like searching, editing and replacing
Basic Grep and Replace Operation
To solve the problem of grep and replace a particular word we will make use of a combination of commands that involves both the above utilities.
Consider we have a text file named textfile.txt with the following content −
this is a simple file
Now, we want to replace the text simple with not a simple. We will first locate the file, then use the exec utility command to tell Linux we want to execute something, followed by the sed command that will help us to grep and change the string in the file.
Using find with sed Command
Run the following command in your terminal −
find textfile.txt -type f -exec sed -i "" "s/simple/not a simple/g" {} \;
After running the command, your content inside the textfile.txt should look like this −
this is a not a simple file
Alternative Methods
Direct sed Command
If you know the exact file location, you can use sed directly −
sed -i "s/simple/not a simple/g" textfile.txt
Using grep with sed
To first verify the word exists, then replace it −
grep -l "simple" textfile.txt | xargs sed -i "s/simple/not a simple/g"
macOS Compatibility
If you are on macOS, you might encounter encoding issues. To resolve this, you need to export two environment variables in your ~/.bashrc or ~/.zshrc file.
Here's an example of environment variables in ~/.zshrc −
immukul@192 linux-questions-code % cat ~/.zshrc export GOPATH=/Users/immukul/go_projects export NDHOME=/Users/immukul/Downloads export GOTRACEBACK=all export GOROOT=/usr/local/go export LC_CTYPE=C export LANG=C
By setting LC_CTYPE=C and LANG=C, we tell the system to treat each byte in strings as its own character without applying any encoding rules. This prevents character encoding issues that can occur on macOS.
Key Points
The
-iflag insedmodifies the file in-placeThe
gflag replaces all occurrences, not just the first oneAlways backup important files before performing bulk replacements
Use double quotes to handle spaces in replacement text
Conclusion
Grep and replace operations in Linux can be efficiently performed using combinations of find, sed, and grep commands. The sed command provides powerful pattern matching and replacement capabilities, while environment variables ensure compatibility across different systems.
