Do you ever have a situation where you need to replace a word or sentence in many files? You could copy and paste it, or perhaps there’s an existing script that can do the job. But how about if you just need to replace it with another word or sentence somewhere else? You could use find, but that would be too generic and require writing a script for this task. And what if you don’t even know what word or sentence you must find and replace? How about using sed then? If you are not familiar with the Unix command line, sed is a stream editor. It’s a simple text editor that reads from standard input and performs editing operations on the stream as it is read. It is commonly used for search and replace functionality in scripts.
In this article, we will show you some examples of how to use sed to perform search and replaces text in files.
Find and Replace String in Files
If you just need to perform a simple search and replace, you can use the following syntax:
sed -i 's/string_to_find/string_to_replace/' file1 file2 fileN
Here:
- -i: This flag indicates to sed that your input file is expected to be one of the files specified after the command.
- string_to_find: This is the string that you want to search for in the files.
- string_to_replace: This is the string that you want to replace the string what_to_find within the files.
- file1 file2 fileN: This is the list of files that sed will search and replace.
The following command will search for all strings “Hello” in the welcome.txt
file. Then replace it with a new string “Howdy” in the same file.
sed -i 's/Hello/Howdy/g' welcome.txt
You can also create a backup of original file before making changes with -i
followed by some extension. For example:
sed -i.backup 's/Hello/Howdy/g' welcome.txt
This will create a file welcome.txt.backup in the current directory with the original file content.
Recursively Replace Text in Multiple files
The Following command is helpful to replace strings in multiple files at once in a directory /opt/docs. Here find will navigate to all files under the defined directory and execute the sed command one by one.
find /opt/docs/ -type f -exec sed -i 's/Hello/Howdy/g' {} ;
Replace Text Without Affecting Original File
If you do not want to make changes in your original file and create new files with changed strings, use the following command.
sed 's/Hello/Howdy/g' welcome.txt > welcome_2.txt
This command will create a new file welcome_2.txt
with modified text. The original file with remain unchanged.
Conclusion
A sed command is a powerful tool that can be used to edit the text in files. It’s useful when you have a situation where you need to find and replace a string in many files. You could use find, but that would be too generic and require writing a script. And what if you don’t even know what word or sentence you must find and replace? Using sed is a better solution.
1 Comment
Hi,
Above command Not Working
find /opt/docs/ -type f -exec sed -i ‘s/Hello/Howdy/g’ {} ;
getting the below error
find: missing argument to `-exec’
solution:
A -exec command must be terminated with a ; (so you usually need to type \;