need help with reading/writing to files

Discussion in 'Ruby' started by Ben, Jan 17, 2005.

  1. Ben

    Ben Guest

    I've been trying to write a program that will take a file, do some
    find/replace operation and save the file. I'm pretty new to ruby, and I
    can't seem to figure out how to do this succinctly. This is my latest
    attempt, but it seems like I shouldn't need to open the file twice.

    I tried to just open the file with "r+" permissions, but if I overwrote
    it with a string that was shorter than the original contents, the extra
    characters would stick around. I couldn't figure out how to slice off
    the extra stuff I didn't need.

    Any suggestions on how to reduce this to one call to File.open???
    Thanks! Any other suggestions are also appreciated. Here's the code...

    print "pattern:"
    pattern = STDIN.gets.chop
    print "replacement:"
    replacement = STDIN.gets.chop
    str = ""
    ARGV.each do |file|
    File.open(file,"r+") do |handle|
    str = handle.read
    end
    File.open(file,"w+") do |handle|
    str.gsub!(/#{pattern}/,replacement)
    handle.write(str)
    end
    end



    Thanks,

    Ben
    Ben, Jan 17, 2005
    #1
    1. Advertising

  2. Ben

    Mark Hubbart Guest

    On Mon, 17 Jan 2005 14:11:07 +0900, Ben <> wrote:
    > I've been trying to write a program that will take a file, do some
    > find/replace operation and save the file. I'm pretty new to ruby, and I
    > can't seem to figure out how to do this succinctly. This is my latest
    > attempt, but it seems like I shouldn't need to open the file twice.
    >
    > I tried to just open the file with "r+" permissions, but if I overwrote
    > it with a string that was shorter than the original contents, the extra
    > characters would stick around. I couldn't figure out how to slice off
    > the extra stuff I didn't need.
    >
    > Any suggestions on how to reduce this to one call to File.open???
    > Thanks! Any other suggestions are also appreciated. Here's the code...
    >
    > print "pattern:"
    > pattern = STDIN.gets.chop
    > print "replacement:"
    > replacement = STDIN.gets.chop
    > str = ""
    > ARGV.each do |file|
    > File.open(file,"r+") do |handle|
    > str = handle.read
    > end
    > File.open(file,"w+") do |handle|
    > str.gsub!(/#{pattern}/,replacement)
    > handle.write(str)

    handle.truncate(handle.pos)
    > end
    > end


    File#truncate(size) truncates the file to the given size. Passing your
    current position after you write to the file truncates it immediately
    after the last character you wrote.

    HTH,
    Mark
    Mark Hubbart, Jan 17, 2005
    #2
    1. Advertising

  3. Ben

    Glenn Parker Guest

    Ben wrote:
    > I've been trying to write a program that will take a file, do some
    > find/replace operation and save the file. I'm pretty new to ruby, and I
    > can't seem to figure out how to do this succinctly. This is my latest
    > attempt, but it seems like I shouldn't need to open the file twice.


    Have you considered doing it all from the command line?

    % ruby -pi -e 'gsub /pattern/, "replacement"' file1 file2...

    --
    Glenn Parker | glenn.parker-AT-comcast.net | <http://www.tetrafoil.com/>
    Glenn Parker, Jan 17, 2005
    #3
  4. Ben

    Ben Guest

    File#truncate was exactly what I was looking for. Thanks! Also, thanks
    for the suggestion about the command line, I might try that as well,
    but I might be making the script a little more complicated so it might
    be a bit much for the command line. Thanks, both! I appreciate it.

    Ben
    Ben, Jan 17, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Modukuri
    Replies:
    0
    Views:
    2,887
    Modukuri
    May 19, 2004
  2. Steve Bennett

    need help reading/writing binary

    Steve Bennett, Aug 25, 2004, in forum: Perl
    Replies:
    1
    Views:
    484
    Joe Smith
    Aug 25, 2004
  3. Ben Jeurissen
    Replies:
    5
    Views:
    417
  4. Rod Richenberg

    help reading/writing from/to excel files

    Rod Richenberg, Jan 23, 2007, in forum: Ruby
    Replies:
    1
    Views:
    102
    Glen Pfeiffer
    Jan 23, 2007
  5. Mad Scientist Jr

    help! trouble reading / writing files in IIS

    Mad Scientist Jr, Apr 1, 2006, in forum: Perl Misc
    Replies:
    6
    Views:
    134
    Dr.Ruud
    Apr 1, 2006
Loading...

Share This Page