Find and Replace using Sed

Sed is a UNIX stream editor that can be used to filter text files. This can be extremely useful if you have to run a Find and Replace on a string of text across a large file. I find this to be much more efficient than using a Find and Replace feature in a text editor. It is much faster (especially on very large files) and you can let it run in a separate Terminal tab without holding up your workflow.

So how does it work? It’s pretty straightforward. You need to know the location of the file you wish to edit relative to your current directory. You will also need to know the string you are looking to update. The syntax looks something like the following:

sed -i ".backup" 's/string-to-find/string-to-replace/g' path/to/file

In the next example of find and replace with SED I must replace a string with a backslash so I use + sign instead of / and this is the result :

  • original_text
  • text_to_replace/withslash
sed -i 's+original_text+text_to_replace/withslash+g' /home/user/website_dump.sql

Thanks to Daniel W. Robert