Monday, June 14, 2010

Ruby script converting to uppercase first char in a "" delimitered string

Here is some ruby script to convert the first charter of each word to uppercase in a sub string delimited by "" on each line

#!/usr/bin/ruby
File.open(ARGV[0], "r") do |infile|
  infile.each_line do |line|
    # / line
    md = /(.*)\"(.*)\"(.*)/.match(line)
    if md
      mid = md[2] 
      mid.gsub!(/ ([a-z])([a-z]*)/) {
        " " + $1.upcase + $2
      }
      line = md[1] + '"' + mid + '"' + md[3]
    end
    puts line
  end
end

No comments:

Post a Comment