アットウィキロゴ

rubyのべんりなscript


xmlを解析する


こんなxmlを解析するrubyスクリプト

<guitars title="My Guitars">
   <make name="Fender">
      <model sn="123456789" year="2006" country="japan">
         <name>62 Reissue Stratocaster</name>
         <price>750.00</price>
         <color>Fiesta Red</color>
      </model>
      <model sn="112233445" year="2007" country="mexico">
         <name>60s Reverse Headstock Stratocaster</name>
         <price>699.00</price>
         <color>Olympic White</color>
      </model>
   </make>
   <make name="Squier">
      <model sn="445322344" year="2003" country="China">
         <name>Standard Stratocaster</name>
         <price>179.99</price>
         <color>Cherry Sunburst</color>
      </model>
   </make>
</guitars>

require "rexml/document"
include REXML    # so that we don’t have to prefix everything
                 # with REXML::...
doc = Document.new File.new("guitars.xml")
#全て表示する
#print doc
#名前を全て表示する
doc.elements.each("guitars/make/model/name"){ |element|
  puts element.text
}
#価格を全て取り出して合計する
total = 0
doc.elements.each("guitars/make/model/price") { |element|
   total += element.text.to_i
}
puts "Total is $" + total.to_s
#属性yearを全て表示
XPath.each( doc, "//model/attribute::year"){ |element|
  puts element
}


置換する

ruby -i.bak rep.rb data.txtって書くと、data.txtにある文字列を置換する。
元のファイルはrep.txt.bakという名前で保存される。

#rep.rb 置換する
while gets
  gsub(/INITIAL *[0-9]*K *NEXT *[0-9]*K/,'INITIAL 8K NEXT 8K')
  print
end


excelファイルをテキストに変換

#! C:\ruby-1.8\bin

require 'win32ole'

fname = ARGV[0]

def getAbsolutePath filename
  fso = WIN32OLE.new('Scripting.FileSystemObject')
  return fso.GetAbsolutePathName(filename)
end
filename = getAbsolutePath(fname)

xl = WIN32OLE.new('Excel.Application')

book = xl.Workbooks.Open(filename)
begin
  puts "<h1>" + book.Name + "</h1>"

  #各シートの処理
  book.Worksheets.each do |sheet|
    puts "<h2>" + sheet.Name + "</h2>"

    #各行の処理
    sheet.UsedRange.Rows.each do |row|

      #各セルの内容を配列に入れる
      record = []
      row.Columns.each do |cell|
        record << cell.Value
      end
      puts record.join

    end
  end
ensure
  book.Close
  xl.Quit
end

excelファイルをgrep

#! C:\ruby-1.8\bin
#######################################
# grepxls.rb: エクセルファイルをgrepする
#     param1: ファイル名
#     param2: grepする文字列
#######################################
require 'win32ole'

#パラメータ取得
fname = ARGV[0]
str = ARGV[1]

re = Regexp.new(str)

def getAbsolutePath filename
  fso = WIN32OLE.new('Scripting.FileSystemObject')
  return fso.GetAbsolutePathName(filename)
end
filename = getAbsolutePath(fname)

xl = WIN32OLE.new('Excel.Application')

book = xl.Workbooks.Open(filename)
begin
  #各シートの処理
  book.Worksheets.each do |sheet|

    rownum = 0
    #各行の処理
    sheet.UsedRange.Rows.each do |row|

      rownum += 1
      #各セルの内容を配列に入れる
      record = []
      row.Columns.each do |cell|
        record << cell.Value
      end

      #puts record.join(",")
      if record.join =~ re then
        puts book.Name + ":" + sheet.Name + ":" + rownum.to_s + ":" + record.join
      end
    end
  end
ensure
  book.Close
  xl.Quit
end
最終更新:2008年02月03日 18:44