From: Robert Klemme Date: 2012-03-05T21:34:43+09:00 Subject: Re: Reading particular text from a txt file Lekha Selvaraj wrote in post #1050186: > I wrote the following code to read a particular line from a file : > > f = File.open('F:\Testing123.txt') > a = f.readlines You read more than necessary. You just need to read 8 lines. > puts a[7] Btw, you are not closing the file properly. http://blog.rubybestpractices.com/posts/rklemme/001-Using_blocks_for_Robustness.html > The Output is : > CRP+TYH+111111' > > What I want : > > I only want 111111 returned from the file. > Can anyone pls tell me the code for that? def r(f) File.open f do |io| io.each_with_index do |line, num| return line[/^CRP\+TYH\+(\d+)/, 1] if num == 7 end end nil end or def r(f) File.open f do |io| line = io.first(8)[-1] and line[/^CRP\+TYH\+(\d+)/, 1] end end Kind regards robert -- Posted via http://www.ruby-forum.com/.