How to use "sed" to find and replace

May
26
2009
Tagged in , , &

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

Use a different delimiter

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 :)

Post new comment

The content of this field is kept private and will not be shown publicly.
  • 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: <pre>, <code>.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.

Follow Me