It's quite a large block of text, you should have broken it down into
more specific tasks. However when you do define functions, such as
capture_task and the like, they really do not do anything useful. This
would also stop you having to have variables named i, i2, i4 etc.

Such names are not very helpful, just how is i4 similar to i2 given
that they have a similar name? In the first option i2 is really
count_of_sublist_items (a long name but at least you know what it
means). i3 is unused.

In the second task i4 is really a count of the line being read from a
file. However you dont actually use it anywhere. If you want the line
count you could use

filelines.each_with_index do |line,index|
 ...
end

as filelines is just a plain array. Remember index will start from 0 and not 1.

Also you don't really need all those break statements, this is not c.