user5739050 user5739050 - 4 months ago 5
Ruby Question

How do I remove something lines from a text file in Ruby?

Every 4 rows leave following 6 lines removed and so until the end of the file.
No rows deleted can be written in another file

file to remove the lines

34
511
6977
511
0
22
20
8569
15
23
6466
390
1
54
9140
-100
0
12
10
5308
19
12
9240
442
1
46
433
55


file after removing lines

34
511
6977
511
6466
390
1
54
19
12
9240
442

Answer

The basis for this is the each_with_index function:

lines.each_with_index do |line, i|
  case (i % 10)
  when 0..3
    puts line
  end
end

You can adapt that code to put the output somewhere else, like an additional array or what have you.