#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0

    Help with search & replace


    This should be a simple search & replace of a string in a raw text file. However, my script is replacing the text, it's just replacing it with an empty string despite my declaration of a specific string.

    First, I have an apple script calling the ruby file to run on the text file:
    set filechoice to choose file with prompt "Select the Export files whose borders need to be replaced."
    --display dialog filechoice

    do shell script "/usr/bin/ruby '/Users/admin/Library/fixes/framefix.rb' " & filechoice

    display dialog "File has been repaired."

    Then, I have the framefix.rb file:
    require 'fileutils'

    #parameters are valid
    arg = ARGV.join(' ')
    arg = arg.gsub(/:/,'/').insert(0,'/Volumes/')
    if arg.nil? || arg.strip.empty? then
    warn "Usage: $0 [filename]"
    exit(-1)
    elsif !File.exists?(arg) then
    warn "File: [%s] Cannot be found." % arg
    exit(-255)
    end

    # Loading up file
    data = File.open(arg).readlines

    keep = Hash.new("")

    # Selects correct border
    old_border = ',100,0.0,10000000,'
    new_border = ',128,'

    data.each do |line|
    line.gsub!(old_border, new_border)
    end

    File.unlink(arg)
    File.open(arg, "w") do |f|
    keep.sort.each do |k, v|
    #warn k
    f.print v
    end
    end

    Any help with this would be greatly appreciated!!
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    this doesn't make a lot of sense to me. What's with the keep hash? You don't fill it anywhere, yet you assume that it contains ... something.
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0
    To be honest, I don't know. We already use a script that is similar which was created by someone who knew Ruby much better than myself. I copied the .rb file and have modified & removed some of the unnecessary coding for this task. Though, this never occurred to me as a potential issue.

    Essentially, all this script should be doing is going through the text file, looking for any string of ',100,0.0,10000000,' and replacing it with ',128,'.

    I removed the coding so that it reads:
    require 'fileutils'

    #parameters are valid
    arg = ARGV.join(' ')
    arg = arg.gsub(/:/,'/').insert(0,'/Volumes/')
    if arg.nil? || arg.strip.empty? then
    warn "Usage: $0 [filename]"
    exit(-1)
    elsif !File.exists?(arg) then
    warn "File: [%s] Cannot be found." % arg
    exit(-255)
    end

    # Loading up file
    data = File.open(arg).readlines

    # Selects correct template
    old_border = ',100,0.0,10000000,'
    new_border = ',128,'

    data.each do |line|
    line.gsub!(old_border, new_border)
    end

    File.unlink(arg)

    However, I am still running across the same issue. It replaces the text, but with an empty string.

    Originally Posted by Jacques1
    Hi,

    this doesn't make a lot of sense to me. What's with the keep hash? You don't fill it anywhere, yet you assume that it contains ... something.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0
    Apparently, if I swap the variables out with the actual data to search and replace, the file gets deleted despite the additional text that should remain.

IMN logo majestic logo threadwatch logo seochat tools logo