changing a files' content

P

Paul Fox

hi all,
i have a text file with various lines, and i want to alter some of these
using regex and gsub.

so i have patterns={/some_pattern/=>"replacement text"}
and i'm thinking this:
in = File.open(filename)
while (line=in.gets)
if line.strip =~ pattern
line.gsub(pattern,patterns[pattern])
end
end

but i'm not sure if that's efficient? and of course, i'm struggling on
the best way of writing those changes back into the file.

any thoughts?
thanks,
f.
 
R

Robert Dober

hi all,
i have a text file with various lines, and i want to alter some of these
using regex and gsub.

so i have patterns={/some_pattern/=>"replacement text"}
and i'm thinking this:
in = File.open(filename)
while (line=in.gets)
if line.strip =~ pattern
line.gsub(pattern,patterns[pattern])
end
end

Maybe you would like to do this

ARGF.each | line |
line.gsub(...) if line.....
print line
end

and then
ruby -i.bup source.rb file1, ...


HTH
Robert
 
P

Paul Fox

Robert said:
ruby -i.bup source.rb file1, ...

thanks for the reply the -i flag looks like it'll be useful, but in my
current situation, i need to be able to do it via an instance method,
rather than command line.
i guess i could call system() to do it, but tht doesn't seem right?
 
R

Robert Dober

thanks for the reply the -i flag looks like it'll be useful, but in my
current situation, i need to be able to do it via an instance method,
rather than command line.
I fail to understand, can you elaborate on the calling environment?
Assuming that you cannot use the -i flag I would be tempted to read
the file into memory or use tempfile to create a copy of the original
file and than open the original file with write access ("w"), if this
is not feasible either I would (as a last resort ) go for read-write
access, this is quite messy at least FWIAC :(.
Hopefully somebody else can give you (us) better information about
that particular issue.

Robert
 
M

MonkeeSage

hi all,
i have a text file with various lines, and i want to alter some of these
using regex and gsub.

so i have patterns={/some_pattern/=>"replacement text"}
and i'm thinking this:
in = File.open(filename)
while (line=in.gets)
if line.strip =~ pattern
line.gsub(pattern,patterns[pattern])
end
end

but i'm not sure if that's efficient? and of course, i'm struggling on
the best way of writing those changes back into the file.

any thoughts?
thanks,
f.

patterns = { /some_pattern/ => "replacement text" }

File.open(filename, 'r+') { | handle |
out = []
handle.readlines { | line |
patterns.each { | pattern, replacement |
if line.strip =~ pattern
line = line.gsub(pattern, replacement)
end
}
out << line
}
handle.write(out.join(''))
}

Untested...

Regards,
Jordan
 
L

lrlebron

hi all,
i have a text file with various lines, and i want to alter some of these
using regex and gsub.

so i have patterns={/some_pattern/=>"replacement text"}
and i'm thinking this:
in = File.open(filename)
while (line=in.gets)
if line.strip =~ pattern
line.gsub(pattern,patterns[pattern])
end
end

but i'm not sure if that's efficient? and of course, i'm struggling on
the best way of writing those changes back into the file.

any thoughts?
thanks,
f.

I did something similar using the rio library. Cleaned up some vb
files in place

require 'rio'
rio('C://test').all.files('*.vb') do |f|
rio(f) <f.contents.gsub(/'UPGRADE.*\s/,"").gsub(/On Error GoTo
ErrHandler/,"Try").gsub(/ErrHandler:/,"Catch ex as Exception\n").gsub(/
Resume Next/,"End Try")
end
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top