|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
How to parse a large tab delimited text file
Hello,
I have a large tab delimited text file provided each day by my importer (about 8 MB). I would like to compare that file with my database by giving a column value. I have tried fgets(), but seems it cannot handle that big file. file() also not working. Any idea? Thanks in advance. |
|
#2
|
|||
|
|||
|
Where is the file, is it on the server or a client pc???
|
|
#3
|
|||
|
|||
|
It resides in a local server.
So, any suggestion?? |
|
#4
|
||||
|
||||
|
if you are comparing it to your database why aren't you importing it to a table and doing the comparison directly in the database itself? or is it more than one table?
what exactly are you trying to compare?
__________________
MySQL - reformat varchar dates to proper date type || MySQL - room vacancies || output results with column names with mysql only Latest row per group || Deleting duplicates || Gaps in auto increment column My site: sql help articles |
|
#5
|
|||
|
|||
|
Yeah, its more than 2 tables.
|
|
#6
|
||||
|
||||
|
The memory limit set in your php.ini can and will effect your processing of large files. You can either change it, use ini_set to increase the memory limit for the processing script, or even better, fopen the file and read/process one line at a time.
If everything is on one line with tabs between items, use fgets with the byte limit and work some magic to reassemble any items cut in half.
__________________
My new WebComic http://www.jjsunshines.com/ The Geek Shall Inherit the Earth It is NOT ok to IM me with questions unless I told you it was ok via PM |
|
#7
|
|||
|
|||
|
Quote:
Use fopen and fread but instead of bringing the whole file in one chunk bring it in pieces. Whats the code you are using now?
__________________
"They have the internet on computers now" - Homer |
|
#8
|
||||
|
||||
|
a better explanation of what exactly you are trying to do with this file in comparison to your database may allow us to give you a more involved and perhaps more accurate answer.
|
|
#9
|
|||
|
|||
|
Thanx guys! Well, for a start, here is the code I used to compare the enries between database and the text file:
PHP Code:
The above code is to demonstrate what I really want to do (please ignore any code error). Well, somehow I made it work with a small size file which may be little bit modified version of the above code. |
|
#10
|
|||
|
|||
|
I dont know if this will work but try bringing in the data in smaller pieces. Something like this...
PHP Code:
Give it a go, see how it works out. |
|
#11
|
|||
|
|||
|
Thanx a lot. But it did not work although it works with a smaller size file. I changed the memory limit too. But no success! Its kinda hang the system!!
|
|
#12
|
||||
|
||||
|
Okay, say you have an 8Mb file and an 8Mb database. The database is _made_ for searching through that much data. You have to come up with some hack to do so with the file and it won't be efficient.
So, I'd do it in this manner. 1. Grab 10 lines out of the file using fgetcsv() (set delimiter to tab, \t) 2. Parse the "$columns[1]" entry out of each line 3. Create a query that'll pull the matching entries out of the database for those 10 columns. Something like SELECT * FROM table WHERE products_id IN (1,2,3,4,5,x) where 1,2,3,etc are the columns pulled from the file. 4. Retrieve the rows from the database one by one, find the matching $columns[x] vlaue and display whatever you need. 5. Retrieve the next 10 rows from the table and GOTO 2. You'll run considerably more queries this way, but that's what databases are designed for. You can tweak the "10" lines of the file to stay within your memory limit, too. Maybe you can do 100, maybe only 5; who knows? I imagine you'll want to use set_time_limit(0), also, so your script doesn't time out. Give it some time to do what it needs to do. ---John Holmes... |
|
#13
|
||||
|
||||
|
Coping with mysql record count limits and script timeouts.
I moved this post to it's own thread. You can find it here.
__________________
A good discussion on screening form input data can be found here. Summary here. Need a competent host? 100gb of web space? Windows AND Unix functionality? ASP.NET AND PHP on the same site? Try Netfirms. Highly recommended by ... me. Save/upload the file BEFORE you test it. Thinking is a luxury afforded by the patience of those you serve. Die with honor ... or don't die at all. Last edited by JoeReeves : February 18th, 2006 at 04:58 PM. |