2008年7月22日 星期二

從文字檔讀入並讀出特定字串那行

只做簡單的比對,若要變更只要更改正規式裡面的條件就好

不過應該有比較簡易的寫法吧?目前是這樣比較容易閱讀 :p

filename = 'input.txt'
output = 'output.txt'

if File.file? filename
  p 'Reading File...'
  open(filename) do |f|
    open(output, 'a') do |out|  
  p 'Writting File...'
      f.to_a.each do |x|
        if x =~ /String!/
          out.puts x
        end
      end

    end # end of output   
  end # end of filename

else
  p "File Open Error!"
end

http://pastie.org/238588

沒有留言: