time displays the time in a separate file. It is necessary to intercept the line and forward it to the end of another file. I do not understand what the catch is ..

File.readlines('time.log', 'r') do |line| if line =~ /^real/ File.new('table.log', 'a').puts(line) end end 

What does not suit me is string trimming:

 real 15,775 

Only numbers are needed. I tried to prescribe the regulars in different ways. but they either didn’t work or brought out something strange

 File.new('table.log', 'a').puts(line[0]) 
  • Understood nothing. Transfer your "answer" (which does not fully suit you) to the question and give a minimal example of values ​​(file, strings). - D-side

2 answers 2

To get the number, there are several ways to choose from:

  • With the help of a simple regular program /\d+,\d+/ (1+ digits,,, 1+ digits)
  • Split a line with a space and take the last element
  • Remove real + spaces at the beginning of a line.

Code example:

 s = 'real 15,775' puts s[/\d+,\d+/] puts s.split(" ").last puts s.sub(/^real\s*/,'') 

See the Ruby demo .

  • Strange, like I tried the first option with the numbers s =~ [/\d+, \d+/] . Deduced the nonsense something like 7 7 7 7 7 6 6 6 6 с новой строки каждое числo . You can refer to inexperience. Thank you. - QWD666
  • By the way, this does not work for the File.new('table.log', 'a').puts(line[0][/\d+,\d+/]) element File.new('table.log', 'a').puts(line[0][/\d+,\d+/]) - QWD666
 line = IO.readlines('time.log') File.new('table.log', 'a').puts(line[0] =~ /[^\w]/) 

Somehow solved the problem. However, it is impossible to pull out only the digits of the format 1.784851.