Pregunta

I have the following snippet of code that I am trying to use to read a file that can have some lines repeated two or more times. The goal of this script is to only write unique lines (no duplicates) but for some reason it appears that it is not detecting equality. Any thoughts?

$handle = @fopen("Old.csv", "r");
$new = @fopen("New.csv", "w");
$last_line = null;

if ($handle && $new) {
    while (($buffer = fgets($handle, 4096)) !== false) {
        if( $last_line != $buffer ) fwrite( $new, $buffer );
        $last_line = $buffer;
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
    fclose($new);
}

Here is an example of "Old.csv"

 apple
 apple
 orange
 grapes
 grapes
 grapes

"New.csv" should be:

apple
orange
grapes

But it ends up being an exact copy of "Old.csv".

¿Fue útil?

Solución

try cat old.csv | sort -u > new.csv at the command prompt its much faster.

Otros consejos

Thanks to everyone to replied. I did unintentionally leave out a clue which is that I am on a Mac. I resaved the CSV to use the Windows format and re-ran my script and all is well. I guess it was the line endings. Anyhow, the bottom line is that the script works.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top