How to delete specific lines in text file?
Solution 1
Deleting lines cleanly and efficiently from a text file is "difficult" in the general case, but can be simple if you can constrain the problem somewhat.
Here are some questions from SO that have asked a similar question:
- How do I remove lines of data in the middle of a text file with Ruby
- Deleting a specific line in a text file?
- Deleting a line in a text file
- Delete a line of information from a text file
There are numerous others, as well.
In your case, if your input file is relatively small, you can easily afford to use the approach that you're using. Really, the only thing that would need to change to meet your criteria is to modify your input file loop and condition to this:
File.open('output.txt', 'w') do |out_file|
File.foreach('input.txt').with_index do |line,line_number|
out_file.puts line if line_number.even? # <== line numbers start at 0
end
end
The changes are to capture the line number, using the with_index
method, which can be used due to the fact that File#foreach
returns an Enumerator
when called without a block; the block now applies to with_index
, and gains the line number as a second block argument. Simply using the line number in your comparison gives you the criteria that you specified.
This approach will scale, even for somewhat large files, whereas solutions that read the entire file into memory have a fairly low upper limit on file size. With this solution, you're more constrained by available disk space and speed at which you can read/write the file; for instance, doing this to space-limited online storage may not work as well as you'd like. Writing to local disk or thumb drive, assuming that you have space available, should be no problem at all.
Solution 2
Use File.readlines
to get an array of the lines in your input file.
input_lines = File.readlines('input.txt')
Then select only those with an even index.
output_lines = input_lines.select.with_index { |_, i| i.even? }
Finally, write those in your output file.
File.open('output.txt', 'w') do |f|
output_lines.each do |line|
f.write line
end
end
Alex Shmatko
Updated on June 18, 2022Comments
-
Alex Shmatko almost 2 years
Suppose, I have an input.txt file with the following text:
First line Second line Third line Fourth line
I want to delete, for example, the second and fourth lines to get this:
First line Third line
So far, I've managed to delete only one the second line using this code
require 'fileutils' File.open('output.txt', 'w') do |out_file| File.foreach('input.txt') do |line| out_file.puts line unless line =~ /Second/ end end FileUtils.mv('output.txt', 'input.txt')
What is the right way to delete multiple lines in text file in Ruby?
-
alexventuraio over 2 yearsIn case anyone needs it, I just changed the second step with
input_lines.pop(number_of_lines)
to remove lines from the end of the file or withinput_lines.shift(number_of_lines)
to remove lines from the beginning of the file and it works like a charm.