Parsing useful subtext from text file, and saving it to Mysql.

Hi there,

I am trying to figure out how to do something in Rails.

Here’s the situation:

I want to store in the DB (as fields) info found in a text file. I’ve created a ruby scripted, where I “isolate” the area with the text I want(just to test, or play around).

I need a guideline on the approach…

I want my user, to upload a text file, and then Rails should use this file to isolate the needed text and store it to the DB.

Any Ideas?

PS. I consider myself a rails newbie, so excuse my unawareness in advance :slight_smile:

Have a nice day,

Yours,

G.

Hi George,

" Isolate the area with the text you want " , example ?

Ideally it looks like you have to grab a book of regular expressions.

Cheers

Vivek

If you have already worked out how you're going to scrape the data from the text file, then the plumbing of uploading a file and post-processing it will depend slightly on the technique that you use to accept the uploaded file and store it. I built something similar (years ago, my first commercial Rails job) that used Paperclip to upload a PDF, and a custom "thumbnail" processor to extract the plain text contents from the file after it was stored on disk. I doubt that the code would be helpful to you, as this was Rails 2.3, and everything has moved a long way from that point. But the key is to accept the file, store it to disk, then in an after_post_process (Paperclip custom life-cycle event) block, do your magic to the uploaded file.

To do this in stages, work through the Readme on https://github.com/thoughtbot/paperclip and get your app to accept the uploaded file. Then read up on custom processors in the Paperclip wiki on Github. If your question is about text processing, then the previous comment about Regular Expressions is indeed where you need to look. I can recommend Mastering Regular Expressions (Friedl, from OReilly Publishing).

Walter

Hi again

" Isolate the area with the text you want " , example ?

This is how:

def read_file(file_name)

temp = File.open(file_name, “r”).read

data = temp.split("\n")

lookup_item = temp.match(/\tSystem Information/)

puts lookup_item.to_s + " <------lookup item"

@my_match = data.index(lookup_item.to_s)

puts data[@my_match + 1]

puts data[@my_match + 2]

return @my_match

end

puts “Start”

puts read_file("./test_info.txt")

puts “End”

Ideally it looks like you have to grab a book of regular expressions.

It was the first thing I did :smiley:

Now I need to find a way to use this^, in my rails project :smiley: