mercredi 24 février 2016

How to print previous line if the current line meet the condition?

I am reading some data from file in the following way.

File.foreach(input_file) do |line|
   # Do Something with each Line
   puts line
   puts line.inspect
end

The get the following output.

helloworld:
prodValue:
version:7
class:[
ratio:
value: ""
stackOverflow:
version:3

#Inspect Output
"helloworld:"
"prodValue:"
"version:7"    
"class:["
"ratio:"
"value: """
"stackOverflow:"
"version:3"

I want to fetch previous line if the next_line.include? 'version:'. For instance, in the above case I want to print prodValue: & StackOverFlow:

Could you please help me with the solution?

Aucun commentaire:

Enregistrer un commentaire