How to use "sed" to find and replace

After Worldpay recently changed to use the RBS system, I was tasked with finding every instance of select.worldpay.com/wcc/purchase in our forms and replacing it with select.wp3.rbsworldpay.com/wcc/purchase. There appeared to be thousands of files and I wasn't going to do it manually…

A quick google later and I found that you can easily use a combination of Grep (to find the files) and sed (a text stream editor) to change the content. Notice how in sed, the find and replace strings have to have the forward slash escaped as we're using it as delimiter.

grep "select.worldpay.com/wcc/purchase" . -Rl | xargs sed -i 's/select.worldpay.com\/wcc\/purchase/select.wp3.rbsworldpay.com\/wcc\/purchase/g'

This probably isn't perfect - but it did the job for me and I hope it helps anyone else that stumbles upon it :)

Comment Icon

1 Comments

The most recent comment was on Fri, 27th Aug 2010 - 19:08

With sed you can choose other delimiters than "/", why not use ";" when working with pathnames?

grep "select.worldpay.com/wcc/purchase" . -Rl | xargs sed -i 's;select.worldpay.com/wcc/purchase;select.wp3.rbsworldpay.com/wcc/purchase;g'

Escaping is a bitch :)

Add new comment

Filtered HTML

  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <img> <p>
  • You can use BBCode tags in the text. URLs will automatically be converted to links.
  • You can enable syntax highlighting of source code with the following tags: <code>, <pre>, <bash>, <css>, <html>, <js>, <jquery>, <mysql>, <php>. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.