-1

How to process the data between the lines starting with File Id one by one. My below code gives output for 1st section , what if there are 100 + sections separated by File Id:.

Its like I want to take the 1st section arrange the data then take up the 2nd section (File ID) and so on. if there is a better way I would surely listen to it. Thanks.

Input file (input.txt)

File Id: C:/my_files/00_Roll_Tom-values.txt

#RakeBoss-Random as on 12/19/2016
[groups]
met = chk\rel_io_chk, chk\dev_op_io,
div = chk\kzhr2x, chk\zz52t0, chk\czzjrt
rakeonly = chk\rzgnsd, chk\cztw5h

[/]
@met = rw
@div = rw
@rakeonly = r

File Id: C:/my_files/Rander-values.txt

#RakeBoss-Jan 21st QA
[groups]
met = chk\rel_io_chk, chk\dev_op_io
div = chk\541kmj, chk\zz52t0
app_only = chk\zz9ycd
check_io = chk\wder4, chk\zz9ycd
div_write = chk\lo98j3
year_on = chk\3w345f
[/]
@met = rw
@div = rw
@app_only= r
@check_io = r
@div_write = rw
@year_on = r

[/wedmin]
@check_io = rw
div_write= rw

[/doc/prod]
@div = rw
@app_only = r
year_on = rw

File Id: C:/my_files/456_Milo_123-values.txt

#RakeBoss-Jan 21st Prod
[groups]
met = chk\rel_io_chk, chk\dev_op_io
div = chk\kzhr2x, chk\zz52t0, chk\czzjrt, chk\pzjwr3
jee_only = chk\zz9ycd, chk\hz659l, chk\zzktgj, 
check_io = chk\8u7y01, chk\zz9ycd
unique_key = chk\zz9ycd
year_on = chk\dytg6
[/]
@met = rw
@div = rw
@rakeonly = r

[/Release]
@check_io = rw
@unique_key = rw

[/Redmine/Treehub]
@div = r
@jee_only = rw

.............
...................
............. so on separated by File Id:

Code

File.open("input.txt", "r") do |f|
  fight_info = f.read
  m = fight_info.match(/File\sId(.+?)File\sId/m)
  puts m
end
rastasheep
  • 10,416
  • 3
  • 27
  • 37
voltas
  • 553
  • 7
  • 24
  • How big can the input file be? If it's guaranteed to always fit into memory you can "slurp" it using `read`, but usually files in production systems shouldn't be handled that way. – the Tin Man Jun 19 '17 at 18:17

2 Answers2

1

You could make a method to abstract the reading of the file and splitting it up into sections:

def split_by_file_id(file)
  lines = []
  id = nil
  File.foreach(file).with_index do |line, line_num|
    if line =~ /^File Id: (.*)/m
      yield id, lines if id
      lines = []
      id = $1
    else
      lines << line if id
    end      
  end
  yield id, lines if id
end

# Code to process each section...
split_by_file_id("input.txt") do |id, lines|
  lines.each do |line|
    # do something...
  end
end
Ginty
  • 3,483
  • 20
  • 24
  • Thanks much, Any idea how to organize data in the [groups] and in [/] Eg, from section 1, scm, dev, readonly should be assigned to array called id with its corresponding values one by one – voltas Jun 19 '17 at 09:35
0
file = File.open("input.txt", "rb")
contents = file.read.split("File Id:")
contents.shift

Now you can do contents.each to loop through it.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • Be _very_ careful doing this. Using `read` pulls in the entire file into memory which can destroy performance. See https://stackoverflow.com/q/25189262/128421 – the Tin Man Jun 19 '17 at 18:20