Thanks! That works of course. It's always something simple (or stupid perhaps)..
-----Original Message----- From: Topher Cyll <[EMAIL PROTECTED]> To: Portland Ruby Brigade <[email protected]> Date: Fri, 13 Jan 2006 12:55:52 -0800 Subject: Re: [PDX.rb] Ruby regex question Correct me if I'm wrong, but aren't you merging the lines into one string in the Perl example, but not in the Ruby example? How about the following code combined with one of the look ahead regexps you mention in your later mail? file = File.new(filename, "r+") text = file.readlines(nil) text.gsub!(REGEXP HERE) file.pos = 0 file.print lines file.truncate(f.pos) -Toph On 1/13/06, Bryan Donovan <[EMAIL PROTECTED]> wrote: > Hi, I'm trying to convert a Perl regex script to Ruby and I'm not sure how to > do one little thing.. > > I have a csv file that has newline characters (^M aka \r\n) in the middle of > the lines. My Perl script that fixes this is: > > foreach $file (@ARGV) { > open(IN,"$file") or die "can't open $file : $!"; > @f=<IN>; > $f=join("",@f); > $f=~s/[\r\n](?!BOB|JOE)//mg; > open(OUT,">$file") or die "can't open $file for writing : $!"; > print OUT $f; > } > > Where BOB and JOE are the only two strings that are expected to start a line. > Basically it's checking for a newline that isn't followed by either BOB or > JOE and deleting them. I'm sure there are other ways to accomplish this, but > it works. So now I'm trying to do this in Ruby, and have the following code > so far: > > ARGV.each do |filename| > open(filename, "r+") do |f| > lines = f.readlines > lines.each do |it| > it.gsub!(/[\r\n](BOB|JOE)/m,'') > end > f.pos = 0 > f.print lines > f.truncate(f.pos) > end > end > > And of course I've tried some variations of it. I'm just not sure what to > use for the lookahead that the "(?" accomplishes in the Perl script. > > Thanks for any help. > Bryan > > > > > _______________________________________________ > PDXRuby mailing list > [email protected] > IRC: #pdx.rb on irc.freenode.net > http://lists.pdxruby.org/mailman/listinfo/pdxruby > _______________________________________________ PDXRuby mailing list [email protected] IRC: #pdx.rb on irc.freenode.net http://lists.pdxruby.org/mailman/listinfo/pdxruby _______________________________________________ PDXRuby mailing list [email protected] IRC: #pdx.rb on irc.freenode.net http://lists.pdxruby.org/mailman/listinfo/pdxruby
