Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Searching large text files

Posted on 2002-03-03
6
Medium Priority
?
274 Views
Last Modified: 2008-03-17
Hey, I have a large text file that I'd like to perform a search function on.  The file will be around 3 + mb big.  I want the search the return the line that the variable resides on.  Here's a small example:

text.txt
--------
line 1 - stuff
line 2 - things, other, misc
line 3 - that this the other
line 4 - this him her

Searching for 'things' will return: 'line 2 - things, other, misc'.
Searching for 'this' will return: 'line 3 - that this the other' & 'line 4 - this him her'.

I tried opening the file & returning the data into an array splitting it by "\n":

search.php
----------
$fulldir = "/myhost/";
$fullpath = $fulldir . "text.txt";

$fp = fopen($fullpath, "r");
$fdata = fread($fp, filesize($fullpath));
fclose($fp);

echo $fdata;

$mlist[] = split("\n",$fdata);

$result = array_search($search,$mlist[],true);

if($result != $null){ print $mlist[$result]; };

 When running the file the server would Hang Up on me.  I'm guessing this is a large operation to run & the server can't handle it?  I'm wondering if anyone can produce a code to search the file & return specific lines that the text matches.  Hopefully a script that doesn't eat the server usage.

It seems to me that opening the file is a big task, if there's another way to scan the file without opening I'd be interested to know.

Thanks
Gunner
0
Comment
Question by:Helpmeplease
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 5

Accepted Solution

by:
andriv earned 900 total points
ID: 6837918
The file() function returns an entire file as an array. Each line is a seperate alement of the array. Now you do not need the extra code to make it an array and may be easier on your server.

$mlist = file($fullpath);

$result = array_search($search,$mlist[],true);

if($result != $null){ print $mlist[$result]; };

If the file is as big as you say no matter what you do will work the server but you can reduce it my reducing the number of things the server has to process.

0
 

Author Comment

by:Helpmeplease
ID: 6838068
I used the file() command & it splits the array great.  Problem is that array_search doesn't search how I explained above.

Put simple - I want a way to check each line to see if "string_a" exists in that line - if it does print the line.

here's some examples but I'd rather not use them:

working example

$x = "1";
$max = count($mlist);
while ($x <= $max) {
  // if ($variable isin $mlist[$x]) { print $mlist[$x]; }
}

the above is IRC script language but I aren't sure how to do it on PHP.  Please help
0
 

Author Comment

by:Helpmeplease
ID: 6838093
ahh it's ok
I managed to sort the rest myself - didn't really need much help after all.  :)

I'll give ya the points anyway.  Look out for another 300 pointer when I get to the next stage. :)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Helpmeplease
ID: 6838096
here's my final script:

<?php

set_time_limit(60);

$path = pathinfo($PATH_TRANSLATED);
$fulldir = $path["dirname"];
$fullpath = $fulldir . "/masterlist.txt";

$mlist = file($fullpath);

for($index=0; $index < count($mlist); $index++){
if (eregi($search, $mlist[$index])) {
$results[] .= $mlist[$index];
}}
$total = count($results);
echo "$total results found<br>";
$x = "0";
$max = count($results);

while($x <= $max){
  echo "$results[$x]<br>";
$x++;
}

?>

0
 

Author Comment

by:Helpmeplease
ID: 6838097
Thanks for help, pointed out one thing I needed :)
0
 
LVL 5

Expert Comment

by:andriv
ID: 6838295
No problem, glad I was able to help!!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question