stacky33 stacky33 - 2 months ago 4
Ruby Question

How to find an expression in text file and process all lines until the next occurrence of the expression and repeat until end of the file using ruby

I have a text file as following sample:

Some comment on the 1st line of the file.

processing date: 31.8.2016
amount: -1.23
currency: EUR
balance: 1234.56
payer reference: /VS123456/SS0011223344/KS1212
type of the transaction: Some type of the transaction 1
additional info: Amount: 1.23 EUR 29.08.2016 Place: 123456789XY



processing date: 30.8.2016
amount: -2.23
currency: EUR
balance: 12345.56
payer reference: /VS123456/SS0011223344/KS1212
type of the transaction: Some type of the transaction 2
additional info: Amount: 2.23 EUR 28.08.2016 Place: 123456789XY



processing date: 29.8.2016
amount: -3.23
currency: EUR
balance: 123456.56
payer reference: /VS123456/SS0011223344/KS1212
type of the transaction: Some type of the transaction 2
additional info: Amount: 2.23 EUR 27.08.2016 Place: 123456789XY


And I need to process the file in ruby so the result will be I will have the values on the right side (31.8.2016, -1.23, EUR, 1234.56, ...) stored in mysql database.

I only achieved returning either 1 occurrence of the line which contains particular string or all the lines using find or find_all, but this is not sufficient as I somehow need to identify the block starting with "processing date:" and ending with "additional info:" and process the values there, then process next block, and next, until the end of the file.

Any hints how to achieve this?

Answer

You can read the file line by line and recognize new record in the fly. It would eliminate potential issues with big files. Take a look at the following code:

record = {}

File.readlines("filename").each do |line|
  if line.start_with?("some key")
    # new record here - save the object if not empty, reinitialize `record` hash
  else
    key, value = split_the_line
    record[key] = value
  end
end