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

x
?
Solved

Code to demonstrate an efficient way to search the last 100 lines of a file containing roughly ten million lines for a sample string. This should function without having to process each line of the fi

Posted on 2009-03-31
2
Medium Priority
?
333 Views
Last Modified: 2013-12-12
Code to demonstrate an efficient way to search the last 100 lines of a file containing roughly ten million lines for a sample string. This should function without having to process each line of the file (and without making use of the tail command or any external system commands). Any of the following languages can be used
0
Comment
Question by:pradipinfo
[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
2 Comments
 
LVL 5

Accepted Solution

by:
Pantalaim0n earned 500 total points
ID: 24038607
How about this? It only needs a rough indication of the maximum length of a string in the file and the number of strings you want to return.
<?php
	$filename        = ".\\longfile.txt";
	$filesize        = filesize($filename);
	$max_line_length = 1024;
	$num_lines       = 100;
	$file = fopen($filename,"r");
	fseek($file,$filesize-($max_line_length*$num_lines));
	$arr = array();
	for ($i=0;$i<$num_lines;$i++) $arr[] = "";
	while (!feof($file)) {
		$line = fgets($file);
		array_splice($arr,0,1);
		$arr[] = $line;
	}
	
	$linenr = 1;
	foreach ($arr as $line) { echo "(".$linenr.") - ".$line."<br />"; $linenr++; }
?>

Open in new window

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

664 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