English 中文(简体)
查阅档案的某些线
原标题:Read Certain Lines from File
  • 时间:2011-10-17 19:08:38
  •  标签:
  • ruby

刚刚进入Ruby,我正试图学习一些基本的读物指挥,而我已经找到任何可靠的来源。

我试图从档案中穿过某些线,直到档案结束。

So in the file where it says FILE_SOURCES I want to read all the sources til end of file, and place them in a file.

我发现,整个档案都印刷,替换档案中的文字,但我想读到档案中的某些部分。

最佳回答

我想这样做(假设你可以把整个档案读到记忆中):

source_lines = IO.readlines( source_file.txt )
start_line   = source_lines.index{ |line| line =~ /SOURCE_LINE/ } + 1
File.open(  other_file.txt ,  w  ) do |f|
  f << source_lines[ start_line..-1 ].join( "
" )
end

相关方法:

  • IO.readlines to read the lines into an array
  • Array#index to find the index of the first line matching a regular expression
  • File.open to create a new file on disk (and automatically close it when done)
  • Array#[] to get the subset of lines from the index to the end

如果你能够把全部档案读到记忆中,那么我对“tadman”基于国家的语种作更简单的改动:

started = false
File.open(  other_file.txt ,  w  ) do |output|
  IO.foreach(  source_file.txt  ) do |line|
    if started then
      output << line
    elsif line =~ /FILE_SOURCES/
      started = true
    end
  end
end

问题回答

通常,如果你试图从一个限定一些how的档案中提取一个小节的话,你会照此办理:

open(filename) do |f|
  state = nil

  while (line = f.gets)
    case (state)
    when nil
      # Look for the line beginning with "FILE_SOURCES"
      if (line.match(/^FILE_SOURCES/))
        state = :sources
      end
    when :sources
      # Stop printing if you hit something starting with "END"
      if (line.match(/^END/))
        state = nil
      else
        print line
      end
    end
  end
end

你可以根据你所复阅的部分档案,从一个州向另一个州转变。

File.open("file_to_read.txt", "r") {|f|
  line = f.gets
  until line.include?("FILE_SOURCES")
    line = f.gets
  end
  File.open("file_to_write.txt", "w") {|new_file|
    f.each_line {|line|
      new_file.puts(line) 
    }
   new_file.close
  }
  f.close 
}

IO的职能没有概念档案中的“线”是什么。 没有任何直截了当的方法在档案中渗入某一条线,你不得不阅读,无视你不需要的路线。





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签