r/learnruby • u/chucky_z Beginner • Feb 10 '15
Handling *extremely* large text files?
Hey /r/learnruby!
I'm just starting to pick up ruby, and I felt it worthwhile to maybe ask this question pre-emptively.
I'm working on a small Sinatra app, but one of the core features I'm looking at is quickly doing a string replace on really big files (5-10GB+, they're raw SQL).
However... the caveat here is that the strings to be replaced will always be in the top ~150 lines or so.
Is there a really efficient way to do this?
1
Upvotes
1
u/cmd-t Feb 10 '15
Yeah, it's called sed. Some stuff can be really hard to do with one set of tools, while another tool can make it easy.
Of course, you could do it in ruby, with IO and File, and stuff, but that might be a lot more difficult.
1
u/mikedao Intermediate Feb 10 '15
The fact that you have to replace the string is tricky. If you look at the CSV documentation, you can see that there's a way to load in a line at a time. So you could pull the first bunch of lines, but to be honest, I'm not sure of a very efficient way to replace the line.
1
u/Nitrodist Feb 10 '15 edited Feb 10 '15
Maybe you can use the
IOclass to stream and write to the file. I don't know what the side effects are of writing to the middle of the file (say you want to append 1k of lines in the middle of the file -- does that mean you have to rewrite to the end of the file all of the data?).http://ruby-doc.org/core-2.2.0/IO.html#method-i-pos-3D
edit: yes, I was correct -- in order to append X data, you'll have to replace to the end of the file. If you're going to be deleting data, you can probably get away with just adding white space. If you're changing table_a to table_b (same number of characters), you can just do that and close the file afterwards.