How to replace text inplace without opening it, using sed

Blog of interesting data center technical tips / tasks / articles which we come across

How to replace text inplace without opening it, using sed

November 8, 2017 Linux SED Uncategorized 0

I have always loved range of text searching and editing tools in Linux.

Be it 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 sed.

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

Leave a Reply

Your email address will not be published. Required fields are marked *