How to replace text inplace without opening it, using sed
I have always loved range of text searching and editing tools in Linux.
grep to search for various patterns in a file. Or
awk to print parts of a file in required format. Or the general purpose
vi editor. Or the stream editor
Am writing this post to highlight just one feature of sed. To replace certain text in a file without having to open the file, and that too replace in place.
The following command is just wonderful. search for a text and replace it in the same file and saves it. It can also make copy of the original file. -i.bak indicates that a copy of the old file should be taken and saved as .bak in same directory.
sed -i.bak ‘s/searchtext/replacetext/’ /mydir/myfilename
Credits to Gabe Berke-Williams