Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete specific lines in text file?

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?

like image 248
Alex Shmatko Avatar asked May 29 '16 22:05

Alex Shmatko


People also ask

How do I get rid of text lines?

Click the line, connector, or shape that you want to delete, and then press Delete. Tip: If you want to delete multiple lines or connectors, select the first line, press and hold Ctrl while you select the other lines, and then press Delete.

How do you remove lines from a text file in Python?

with open(inputFile, 'w') as filedata: Traverse in each line of the file using the for loop. Enter the line number to be deleted as dynamic input using the input() function (The input() function reads a line from the input (from the user), converts it to a string by eliminating the trailing newline, and returns it.


2 Answers

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.

like image 179
Michael Gaskill Avatar answered Sep 24 '22 12:09

Michael Gaskill


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
like image 24
floum Avatar answered Sep 22 '22 12:09

floum