How to read lines of a file in Ruby

RubyLine Endings

Ruby Problem Overview


I was trying to use the following code to read lines from a file. But when reading a file, the contents are all in one line:

line_num=0
File.open('xxx.txt').each do |line|
  print "#{line_num += 1} #{line}"
end

But this file prints each line separately.


I have to use stdin, like ruby my_prog.rb < file.txt, where I can't assume what the line-ending character is that the file uses. How can I handle it?

Ruby Solutions


Solution 1 - Ruby

Ruby does have a method for this:

File.readlines('foo').each do |line|
    puts(line)
end

http://ruby-doc.org/core-1.9.3/IO.html#method-c-readlines

Solution 2 - Ruby

File.foreach(filename).with_index do |line, line_num|
   puts "#{line_num}: #{line}"
end

This will execute the given block for each line in the file without slurping the entire file into memory. See: IO::foreach.

Solution 3 - Ruby

I believe my answer covers your new concerns about handling any type of line endings since both "\r\n" and "\r" are converted to Linux standard "\n" before parsing the lines.

To support the "\r" EOL character along with the regular "\n", and "\r\n" from Windows, here's what I would do:

line_num=0
text=File.open('xxx.txt').read
text.gsub!(/\r\n?/, "\n")
text.each_line do |line|
  print "#{line_num += 1} #{line}"
end

Of course this could be a bad idea on very large files since it means loading the whole file into memory.

Solution 4 - Ruby

Your first file has Mac Classic line endings (that’s "\r" instead of the usual "\n"). Open it with

File.open('foo').each(sep="\r") do |line|

to specify the line endings.

Solution 5 - Ruby

I'm partial to the following approach for files that have headers:

File.open(file, "r") do |fh|
	header = fh.readline
	# Process the header
	while(line = fh.gets) != nil
        #do stuff
	end
end

This allows you to process a header line (or lines) differently than the content lines.

Solution 6 - Ruby

It is because of the endlines in each lines. Use the chomp method in ruby to delete the endline '\n' or 'r' at the end.

line_num=0
File.open('xxx.txt').each do |line|
  print "#{line_num += 1} #{line.chomp}"
end

Solution 7 - Ruby

how about gets ?

myFile=File.open("paths_to_file","r")
while(line=myFile.gets)
 //do stuff with line
end

Solution 8 - Ruby

Don't forget that if you are concerned about reading in a file that might have huge lines that could swamp your RAM during runtime, you can always read the file piece-meal. See "Why slurping a file is bad".

File.open('file_path', 'rb') do |io|
  while chunk = io.read(16 * 1024) do
    something_with_the chunk
    # like stream it across a network
    # or write it to another file:
    # other_io.write chunk
  end
end

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondrawView Question on Stackoverflow
Solution 1 - RubyJonathanView Answer on Stackoverflow
Solution 2 - RubytalabesView Answer on Stackoverflow
Solution 3 - RubyOlivier L.View Answer on Stackoverflow
Solution 4 - RubyJosh LeeView Answer on Stackoverflow
Solution 5 - RubyRon GejmanView Answer on Stackoverflow
Solution 6 - RubySreenivasan ACView Answer on Stackoverflow
Solution 7 - RubyJBoyView Answer on Stackoverflow
Solution 8 - RubyNelsView Answer on Stackoverflow