473,385 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Function like file() but will read lines X through Y only?

file() works great, until you start looking at file that are very large,
say 134MB.

Is there any function that will return an array for each line of a file,
but only for lines given as parameters?

A function like file("/path/to/file",100,230) which would return lines 100
through 230 as an array.
--
[ Sugapablo ]
[ http://www.sugapablo.net <--personal | http://www.sugapablo.com <--music ]
[ http://www.2ra.org <--political | http://www.subuse.net <--discuss ]

Jul 17 '05 #1
5 2142
Sugapablo wrote:
file() works great, until you start looking at file that are very large,
say 134MB.

Is there any function that will return an array for each line of a file,
but only for lines given as parameters?

A function like file("/path/to/file",100,230) which would return lines 100
through 230 as an array.


Nope. Have to write it yourself.

There is a lot of lazy ways to do it though. PHP (fopen/fgets), piping
from head to tail, sed, awk, grep.

The shortest to write, given your example, is:

sed -n -e 231q -e 100,+130p '/path/to/file'
But not the best choice for performance in all cases. If you only need
to read a small amount of data from the beginning of the file, then use
fopen/fgets in PHP to fetch the lines you need. Even though PHP is slow
compared to the shell-utils, running shellcommands from PHP is costly
enough to sometimes opt for PHP.

If you need to fetch lines from anywhere in the file, then I think grep
would be better. Not sure how head with tail would perfom, or if some
other tool exist.

Those are the lazy ways. If performance is still a problem, then you
need to do other things.
/Bent
Jul 17 '05 #2
Sugapablo wrote:
file() works great, until you start looking at file that are very
large, say 134MB.

Is there any function that will return an array for each line of a
file, but only for lines given as parameters?

A function like file("/path/to/file",100,230) which would return
lines 100 through 230 as an array.


You could try something like the following, which will not take as many
resources, but isn't really fast either:

<?php

function file_from_offset($file, $start, $end) {
if (!($fp = @fopen($file, "r"))) return false;
$lines = array();
$count = -1;
while (!feof($fp) && (++$count <= $end)) {
$line = fgets($fp, 4096);
if (($count >= $start) && ($count <= $end)) {
$lines[] = $line;
}
}
fclose($fp);
return $lines;
}

print_r(file_from_offset("http://www.google.com/", 10, 13));

?>

JW

Jul 17 '05 #3
Sugapablo wrote:
file() works great, until you start looking at file that are very large,
say 134MB.

Is there any function that will return an array for each line of a file,
but only for lines given as parameters?

A function like file("/path/to/file",100,230) which would return lines 100
through 230 as an array.


Sugapablo,

Someone else has proposed some code for doing what you want and
mentioned that it won't be very fast.

If performance is a consideration you would be better served
using an RDBMS.

HTH
Jerry
Jul 17 '05 #4

Sugapablo wrote:
file() works great, until you start looking at file that are very large, say 134MB.

Is there any function that will return an array for each line of a file, but only for lines given as parameters?

A function like file("/path/to/file",100,230) which would return lines 100 through 230 as an array.
--
[ Sugapablo ] [ http://www.sugapablo.net <--personal | http://www.sugapablo.com <--music ] [ http://www.2ra.org <--political | http://www.subuse.net

<--discuss ]

Unless each line contains the same number of characters, you can't read
specific lines within the middle of the file without first reading
everything that comes before.

Jul 17 '05 #5
jerry gitomer wrote:
[snip]
Sugapablo,

Someone else has proposed some code for doing what you want and
mentioned that it won't be very fast.

[snip]

Yup.

I tried different quick and dirty methods, picking out some lines at
different places from a text file with about a million lines (approx.
25MB). ("h|t" is head on file piped to tail)

+---------+---------+-------+-------+-------+-------+-------+
| from | to | PHP | h|t | sed | awk | grep |
+---------+---------+-------+-------+-------+-------+-------+
| 5000 | 5200 | 0.023 | 0.350 | 0.330 | 0.366 | 0.400 |
| 305000 | 305200 | 1.604 | 0.537 | 0.992 | 0.623 | 0.444 |
| 605000 | 605200 | 3.171 | 0.677 | 1.679 | 0.985 | 0.537 |
| 905000 | 905200 | 4.705 | 0.835 | 2.373 | 1.290 | 0.606 |
+---------+---------+-------+-------+-------+-------+-------+

Speedtest is only run once, so not super accurate, but I think
the trend is obvious.
/Bent
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: deko | last post by:
I have a text file ("eighty.txt") that looks like this: 83|84|85|86 I can read the file into an array like this: $numbers= file("eighty.txt"); But how do I key the array? I'd like to use...
9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
2
by: Count Dracula | last post by:
Is there a way to write a c++ program that will print out the last few lines of a file without reading the whole file? The implementations of 'tail' I have seen all appear to be system dependent....
11
by: Ken Varn | last post by:
I want to be able to determine my current line, file, and function in my C# application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___ macros for getting this, but I cannot find a...
23
by: Anunay | last post by:
Hi all, Suppose a text file is given and we have to write a program that will return a random line from the file. This can be easily done. But what if the text file is too big and can't fit...
24
by: rudranee | last post by:
hi there, can anyone tell me how to lines from a file which are odd numbered i.e. 1st,3rd,5th...lines. i tried incrementing file pointer by 2 (fp=fp+2) but it does'nt work Can someone give me...
6
by: Robbie Hatley | last post by:
I'm maintaining a software project with 134 C++ files, some of them huge (as much as 10,000 lines each), and very few prototypes. The author's attitude towards prototypes was like this: ...
1
by: jef.d | last post by:
I am attempting to read through a text file & then update an HTML page table w/ the output from the text file (ie; statusing by table). What I want the code to do is read through the file, look...
2
by: rka77 | last post by:
Hi, I am trying to make a Python2.6 script on a Win32 that will read all the text files stored in a directory and print only the lines containing actual data. A sample file - Set : 1 Date:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.