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 !!!

 

 

 

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.