Ruby replace weirdly encoded string without gsub

Having a bunch of files with weird encoding you need to replace something in ?
Pru to the rescue!
(because replacing with ruby-regex or sed leads to encoding exceptions)

find test -name "*" | pru 'return unless File.file?(self); c =; s = "SEARCH"; r = "REPLACE"; while x = c.index(s) do; c.slice!(x,s.size); c.insert(x, r); end; File.write(self, c)'

Normal ruby:

String.class_eval do
  def string_sub(s,r)
    while x = index(s) do
      insert(x, r)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s