Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6223811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:33:16+00:00 2026-05-24T08:33:16+00:00

I am trying to do a simple find/replace on all text files in a

  • 0

I am trying to do a simple find/replace on all text files in a directory, modifying any instance of [RAVEN_START: by inserting a string (in this case ‘raven was here’) before the line.

Here is the entire ruby program:

#!/usr/bin/env ruby
require 'rubygems'
require 'fileutils' #for FileUtils.mv('your file', 'new location')

class RavenParser

  rawDir = Dir.glob("*.txt")
  count = 0
  rawDir.each do |ravFile|
    #we have selected every text file, so now we have to search through the file
    #and make the needed changes.
    rav = File.open(ravFile, "r+") do |modRav|
      #Now we've opened the file, and we need to do the operations.
        if modRav            
        lines = File.open(modRav).readlines
        lines.each { |line|
        if line.match /\[RAVEN_START:.*\]/ 
          line.gsub!(/\[RAVEN_START:/, 'raven was here '+line)
          count = count + 1
        end
        }
         printf("Total Changed: %d\n",count)
      else
         printf("No txt files found. \n")
      end
    end
    #end of file replacing instructions.
  end

  # S
end

The program runs and compiles fine, but when I open up the text file, there has been no change to any of the text within the file. count increments properly (that is, it is equal to the number of instances of [RAVEN_START: across all the files), but the actual substitution is failing to take place (or at least not saving the changes).

Is my syntax on the gsub! incorrect? Am I doing something else wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:33:18+00:00Added an answer on May 24, 2026 at 8:33 am

    You’re reading the data, updating it, and then neglecting to write it back to the file. You need something like:

    # And save the modified lines.
    File.open(modRav, 'w') { |f| f.puts lines.join("\n") }
    

    immediately before or after this:

    printf("Total Changed: %d\n",count)
    

    As DMG notes below, just overwriting the file isn’t properly paranoid as you could be interrupted in the middle of the write and lose data. If you want to be paranoid (which all of us should be because they really are out to get us), then you want to write to a temporary file and then do an atomic rename to replace the original file the new one. A rename generally only works when you stay within a single file system as there is no guarantee that the OS’s temp directory (which Tempfile uses by default) will be on the same file system as modRav so File.rename might not even be an option with a Tempfile unless precautions are taken. But the Tempfile constructor takes a tmpdir parameter so we’re saved:

    modRavDir = File.dirname(File.realpath(modRav))
    tmp       = Tempfile.new(modRav, modRavDir)
    tmp.write(lines.join("\n"))
    tmp.close
    File.rename(tmp.path, modRav)
    

    You might want to stick that in a separate method (safe_save(modRav, lines) perhaps) to avoid further cluttering your block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to do a simple find and replace within a string with:
I'm trying to find a simple way to change the colour of the text
I'm trying to make a simple php script to find all src attributes from
I'm trying to filter ALL widget output through a simple filter, but can't find
i am trying to make a very simple bash script to find files matching
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
I'm trying to find a simple MySQL statement for the following two problems: I
What I want to do is very simple but I'm trying to find the
I'm trying to replace a small part of text in a large HTML document
Annoying problem. I am trying to replace all semicolon characters in my Model's description

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.