Remove duplicates except 1st occurrence in txt file

Discussion in 'PHP' started by scriptglue, Oct 21, 2008.

  1. #1
    I need a php code example to remove duplicates except 1st occurrence from a txt file (see example1) and save the removed lines (see example2) to a new.txt file.


    Example 1
    blackdog|black dog
    blackfrog|black dog
    blackflag|black dog
    whitecat|white cat
    whiterat|white cat
    whitehat|white cat

    Example 2
    blackdog|black dog
    whitecat|white cat

    Thanks in advance :)
     
    scriptglue, Oct 21, 2008 IP
  2. startwall

    startwall Guest

    Messages:
    167
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i dont know the php but if you have excel it has a duplicates filter
     
    startwall, Oct 21, 2008 IP
  3. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    uh
    $lines = explode("\n",$text);
    $newtext = "";
    $newlines = array();
    foreach ($lines as $line) {
    $terms = explode("|",$line,2);
    if(!in_array($terms[0],$newlines)) {
    $newlines[] = $terms[0];
    $newtext .= $line."\n";
    }
    }


    untested
     
    Kyosys, Oct 21, 2008 IP
  4. scriptglue

    scriptglue Banned

    Messages:
    170
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #4

    Thanks but this doesnt work, it only copies the data from $text to $newtext

    <?php

    function make_content_file($filename,$content,$opentype="w"){
    $fp_file = fopen($filename, $opentype);
    fputs($fp_file, $content);
    fclose($fp_file);
    }


    $text = file_get_contents("done_bad.txt");
    $lines = explode("\n",$text);
    $newtext = "";
    $newlines = array();
    foreach ($lines as $line) {
    $terms = explode("|",$line,2);
    if(!in_array($terms[0],$newlines)) {
    $newlines[] = $terms[0];
    $newtext .= $line."\n";
    make_content_file("done_good.txt",$newtext);
    }
    }

    ?>
     
    scriptglue, Oct 21, 2008 IP
  5. scriptglue

    scriptglue Banned

    Messages:
    170
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Let me rephrase what Im trying to do again

    Remove lines in a csv file IF the duplicates are found in the 2nd column except first occurrence
     
    scriptglue, Oct 21, 2008 IP
  6. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #6
    i didn't test the script, so it might not be working, but I know that you are using it wrong.


    <?php

    function make_content_file($filename,$content,$opentype="w"){
    $fp_file = fopen($filename, $opentype);
    fputs($fp_file, $content);
    fclose($fp_file);
    }


    $text = file_get_contents("done_bad.txt");
    $lines = explode("\n",$text);
    $newtext = "";
    $newlines = array();
    foreach ($lines as $line) {
    $terms = explode("|",$line,2);
    if(!in_array($terms[1],$newlines)) {
    $newlines[] = $terms[1];
    $newtext .= $line."\n";
    }
    }
    make_content_file("done_good.txt",$newtext);

    ?>


    try that
     
    Kyosys, Oct 21, 2008 IP
  7. scriptglue

    scriptglue Banned

    Messages:
    170
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks for your help Kyosys, It works great
     
    scriptglue, Oct 21, 2008 IP
  8. scriptglue

    scriptglue Banned

    Messages:
    170
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #8
    How would I make another file.txt that would save the removed (duplicate) lines?
     
    scriptglue, Oct 21, 2008 IP
  9. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #9
    <?php

    function make_content_file($filename,$content,$opentype="w"){
    $fp_file = fopen($filename, $opentype);
    fputs($fp_file, $content);
    fclose($fp_file);
    }


    $text = file_get_contents("done_bad.txt");
    $lines = explode("\n",$text);
    $newtext = $rest = "";
    $newlines = array();
    foreach ($lines as $line) {
    $terms = explode("|",$line,2);
    if(!in_array($terms[1],$newlines)) {
    $newlines[] = $terms[1];
    $newtext .= $line."\n";
    } else {
    $rest .= $line."\n";
    }
    }
    make_content_file("done_good.txt",$newtext);
    make_content_file("done_bad.txt",$rest);

    ?>

    try that


    Oh, and you might want to look into the function file_put_contents(), it does pretty much the same as your make_content_file
     
    Kyosys, Oct 22, 2008 IP