Jump to content

[SOLVED] Removing lines from a file is lines exists in both file (like a remove list)


tiganulmeu

Recommended Posts

Hy guys, ok, i have two files, a.txt and b.txt, both files contains numbers like :

 

6541664\n

1598455\n

4215324\n

1251254\n

 

and so on, through "\n" i want to specify that these numbers are separated with new lines but i`m sure you already know that. The problem consists in the file size, i want to remove the lines that already exists in file b.txt from a.txt however both files are verry large, i`m talking about at least 10Mb zise each. I did use a function like:

 

function remove_rez($key, $fisier) { $fc=file($fisier); $f=fopen($fisier,"w"); foreach($fc as $line) { if (!strstr($line,$key)) { fputs($f,$line); } } fclose($f); }

$b_file_contents = explode("\n", get_file_contents("b.txt"));

$a_file =get_file_contents("a.txt");

foreach(array_unique($b_file_contents) as $b_file_line) {

if(strpos($b_file_line, $a_file) === TRUE) { remove_rez($b_file_line, "a.txt"); }

}

 

but with laaarge files and lots of repeating values it takes a verrrrrry long time and it even blocks my pc ...... a faster and better solution is greatly appreciated. Thanks in advance !!!

 

 

 

<?php
$A = fopen('a.txt', 'r');
$AR = fread($A, filesize('a.txt'));
fclose($A);
$Explode = explode('\n', $AR);
$B = fopen('b.txt', 'w');
$BR = fread($B, filesize('b.txt'));
$i = 1;
while($i < count($Explode)) {
    if(strstr($i, $BR)) {
        str_replace($i, '', $BR);
    }
}
fclose($B);
//Code

i think that your code trecool999 is still a time consuming script as it still takes each variable in a.txt file to search in b.txt file, i found an array function and i think that it resolves my problem, i only tried it with smaller a.txt and b.txt files, please tell me if this will be ok with large files too ... before i try implementing it in my scripts.... :

 

$a_file_array = explode("\n", file_get_contents("a.txt"));

$b_file_array = explode("\n", file_get_contents("b.txt"));

print_r(array_diff($a_file_array, $b_file_array));

 

 

Thank you verry much for your help.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.