Search and Replace in files!!!!

S

sharath

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.
 
J

James H.

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
 
R

rio4ruby

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
 
R

Ross Bamford

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}')}}"
 
S

sharath

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.
 
T

Trans

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.
 
S

sharath

Hi All,

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

Thanks,
Sharath
 
R

Robert Klemme

Trans said:
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
 
R

Ross Bamford

Hi All,

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

Depends on your platform, but maybe something like:

find -name '*.jsp' -print0 | xargs -0 <<your ruby command>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top