D
D
ds00lon2015-10-23 17:47:35
PHP
ds00lon, 2015-10-23 17:47:35

What is the least resource-intensive way to get a line by line number from a txt file of two million lines?

The first thing that comes to mind is file_get_contents into an array and get it from there by index. But there is an understanding that this is complete dibelism.
How to get, for example, the line 600334 from a file without reading the entire file and without taking up memory for it?

Answer the question

In order to leave comments, you need to log in

8 answer(s)
M
Maxim Grechushnikov, 2015-10-23
@maxyc_webber

_no_ functions that extract a specific string from a file,
because most operating systems don't care about the contents of a file.
(in other words, there is no line-by-line seek)
i.e. to read the k-th line from the file, you need to read the first, then the second, then, etc.
how you do it depends on the language.
I would read $n characters from the file, check for the presence of the \n character, if this is not the right line, then continue. until I find the \n I need , you
can, as an option, make an "index" map. on the first run, store the position of all \n in the file in the file. and then it will be easier to find)
another option would be to run the database into sql / noSql and dance there already

S
Stalker_RED, 2015-10-23
@Stalker_RED

You can read one line at a time with fgets() .
But if the line is somewhere at the end of the file, it can be long. If such an operation needs to be carried out often, it may be worth somehow indexing the file, writing offsets every 100 lines, for example. Then, using fread(), read the desired block, and already iterate over the lines in it.

M
Mikhail Osher, 2015-10-23
@miraage

I'm a pervert, but if the file is very large - I would try sed -n '600334p' file.

A
Aleksey Ratnikov, 2015-10-23
@mahoho

$string = exec("sed '5!d' /path/to/file");
echo $string;

where 5 is the line number

V
VovanZ, 2015-10-23
@VovanZ

fgets reads until newline or end of file by default.
If n is the number of the desired line, then first we call fgets n-1 times (the result is not saved anywhere, so as not to take up memory), the next call to fgets will return the desired line.

D
Dmitry, 2015-10-23
@mytmid

Not only "file_get_contents into an array" is idiotic, but the file itself with 1 million lines...
Split the file into many parts and work with a specific "small" file... You can
store files (line by line), for example, like this:
root_dir/ 100/200/333.txt
where 100 and 200 are directories, and 333.txt is a file with the required line (#100200333).
root_dir/000/600/334.txt
- line 600334
PS It looks "clumsy", but better and faster than one file in a million lines :)

P
Puma Thailand, 2015-10-23
@opium

Put
the file in the database and get a line by id with super speed

N
Nikita Gushchin, 2015-10-23
@iNikNik

Try to make an index. In VKontakte, for example, it also combines all files with user content into one large file and then gives the desired piece.
If you have, for example, int32 in each line, then just calculate the desired offset, then call fseek and voila!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question