Sed is known as Stream Editor, a tool is designed to find and replace text in the text file. It’s very useful like you need to change any word in multiple files and you don’t want to do it manually because of its time killing.
Replace Text in Same Single
The following command will search for all string “Hello” in /opt/docs/welcome.txt file. The replace it with “Howdy” in the same file.
sed -i 's/Hello/Howdy/g' /opt/docs/welcome.txt
Replace Text in All files
The Following command is helpful to replace string in multiple files at once in a directory /opt/docs. Here find will navigate to all files under the defined directory and execute sed command one by one.
find /opt/docs/ -type f -exec sed -i 's/Hello/Howdy/g' {} ;
Replace Text with Keep Original
If you do not want to make changes in your original file and create new files with changed strings, use following command.
sed 's/Hello/Howdy/g' /opt/docs/welcome.txt > /opt/docs/welcome_2.txt
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 \;