[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Search and Replace in files!!!!

sharath

5/22/2006 7:26:00 PM

Hi All,

I'm trying to search for a given string and replacing that string in
all the files wherever it finds. Please find the program as below:

**************************(SearchReplace.rb)***********************
reg_exp =
Regexp.new('(<object[^\r\n>]*>|<applet[^\r\n>]*>)',Regexp::IGNORECASE)
out_file_nm = "output.txt"
out_file = File.new(out_file_nm, "w")

Dir['**/*.jsp'].each do |path|
puts path
File.open(path,"r+" ) do |f|
f.grep(reg_exp) do |line|
puts path, ':', line
out_file.write path +"\n"
f.write line.sub(reg_exp) {|s| 'show(\''+s+'\')' }
end
f.close
end
end

out_file.close
**********************************************************

This program finding the string correctly but it is not replacing
the string. Please let me know how to replace the line after it finds
the string???

Thanks,
Sharath.

12 Answers

James Herdman

5/22/2006 7:56:00 PM

0

I'm not entirely sure you can do in-file replacement, but this line is
key:

f.write line.sub(reg_exp) {|s| 'show(\''+s+'\')' }

line.sub is non-destructive. Have you tried the sub! method?

James H

rio4ruby

5/23/2006 4:59:00 AM

0

You might want to consider using Rio (http:/rio.rubyforge.org) for
this type of problem:

require 'rio'

rio('adir').all.files('*.jsp') do |f|
rio(f) < f.contents.gsub(/search/,'replace')
end

Ross Bamford

5/23/2006 7:32:00 AM

0

On Mon, 22 May 2006 20:26:08 +0100, sharath <sharath.reddy@isrsurveys.com>
wrote:

> Hi All,
>
> I'm trying to search for a given string and replacing that string in
> all the files wherever it finds. Please find the program as below:
>
> [... elided ...]
>
> This program finding the string correctly but it is not replacing
> the string. Please let me know how to replace the line after it finds
> the string???
>

This doesn't give the informational output your program had, but it does
get the job done and changes the files inplace (also creating a backup):

$ ruby -ni.bak -e "print
gsub(/(<object[^\r\n>]*>|<applet[^\r\n>]*>)/i){|s| %{show('#{s}')}}"

--
Ross Bamford - rosco@roscopeco.remove.co.uk

Trans

5/23/2006 1:29:00 PM

0

sharath

5/23/2006 4:05:00 PM

0

Hi All,

It is working fine with the below command. It is changing the files
in the directory where it is execting(not doing recursive directories)

ruby -i.bak -pe "print
gsub(/(<object[^\r\n>]*>|<applet[^\r\n>]*>)/i){|s| %{show('#{s}')}}"
*.jsp

I'm new to ruby and I don't have much information regarding this
command. Can you please let me know some sites where I can know more
about this command internals??

Thanks,
Sharath.

Trans

5/23/2006 5:38:00 PM

0

On the command line do

$ ruby --help

from there you should be able to piece it together. FYI this is stuff
Ruby borrowed from Perl.

T.

William James

5/23/2006 6:44:00 PM

0

Trans wrote:
> one-liner:
>
> ruby -pi -w -e 'gsub!(/search/, "replace")' *.rb

ruby -i -pwe 'gsub(/search/, "replace")' *.rb

sharath

5/23/2006 10:32:00 PM

0

Hi All,

How to run this command so that it will be excuted in all the
subdirectories too....

Thanks,
Sharath

Trans

5/24/2006 4:18:00 AM

0

I believe you can use **/*.rb --although that may ony go one layer
down.

T.

Robert Klemme

5/24/2006 7:05:00 AM

0

Trans wrote:
> I believe you can use **/*.rb --although that may ony go one layer
> down.

You need a zsh for this to work recursively. Alternatively use
Dir['**/*'] inside the script but then the one liner becomes more ugly.
Again alternatively use find

find . -type f -print0 | xargs -r0 ruby -e '...'

robert