Count the number of lines in a file with Ruby, without reading entire file into memory

Posted by smnirven on Stack Overflow See other posts from Stack Overflow or by smnirven
Published on 2010-04-16T04:02:31Z Indexed on 2010/04/16 4:13 UTC
Read the original article Hit count: 226

Filed under:

I'm processing huge data files (millions of lines each). Before I start processing I'd like to get a count of the number of lines in the file, so I can then indicate how far along the processing is. I am using Ruby, and because of the size of the files, it would not be practical to read the entire file into memory just to count how many lines there are. Does anyone have a good suggestion on how to do this?

© Stack Overflow or respective owner

Related posts about ruby