syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me

Question:
How do I use Ruby to edit several files?

Recently I encountered a use-case where I wanted to edit several hundred text files in a directory. Also the logic driving each of the edits was complex.

Ruby is well suited for this type of task.

Usually sed is well suited for pragmatically editing many files but it lacks the ability to respond to complex logic as well as Ruby can.

To demonstrate how to use Ruby to edit several files, assume we have three files in /tmp/:

dan@hp /tmp $ 
dan@hp /tmp $ ll f?.html
-rw-rw-r-- 1 dan dan 11 Dec  3 05:33 f1.html
-rw-rw-r-- 1 dan dan  8 Dec  3 05:34 f2.html
-rw-rw-r-- 1 dan dan 10 Dec  3 05:34 f3.html
dan@hp /tmp $ 
dan@hp /tmp $ cat f1.html
google.com
dan@hp /tmp $ cat f2.html
ibm.com
dan@hp /tmp $ cat f3.html
wired.com
dan@hp /tmp $ 
dan@hp /tmp $ 
Next, I show a script which changes the domain name inside each file into an anchor tag which refers to the domain.
# ruby_edit_file.rb

# This script should change some domains to some HTML-anchor-elements.

require 'tempfile'
require 'fileutils'

somefiles = Dir["/tmp/f?.html"]

somefiles.each {|fn|
  tmpfile = Tempfile.new('tmpfile.html')
  File.open(fn, 'r') do |afile|
    afile.each_line{ |line|
      domain = "http://#{line.chomp}"
      anchor_elem = "<a href='#{domain}'>#{domain}</a>"
      tmpfile.puts anchor_elem
    } # afile.each_line
  end # fn now closed
  tmpfile.close
  FileUtils.mv(tmpfile.path, fn)
}
Here is the result:

dan@hp /tmp $ 
dan@hp /tmp $ cat f1.html
google.com
dan@hp /tmp $ cat f2.html
ibm.com
dan@hp /tmp $ cat f3.html
wired.com
dan@hp /tmp $ 
dan@hp /tmp $ 
dan@hp /tmp $ ruby ruby_edit_file.rb
dan@hp /tmp $ 
dan@hp /tmp $ cat f1.html 
<a href='http://google.com'>http://google.com</a>
dan@hp /tmp $ cat f2.html
<a href='http://ibm.com'>http://ibm.com</a>
dan@hp /tmp $ cat f3.html
<a href='http://wired.com'>http://wired.com</a>
dan@hp /tmp $ 
dan@hp /tmp $ 
Ruby is well suited for editing files in a way which depends on complex logic.


syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me