Page 1 of 1

Compare two text files, output line matches

Posted: Thu Dec 16, 2010 5:58 pm
by Engineerd
I know this sounds simple, but I can't seem to properly compare lines from, say, two csv files and write the matches to another file.

Any help?

Love you guys btw.

Re: Compare two text files, output line matches

Posted: Thu Dec 16, 2010 10:51 pm
by McInfo
If you're looking for identical lines rather than changes that may have been made, there is a good chance this will work.

Code: Select all

// Puts matching lines from files A and B into C
file_put_contents('c', implode(array_intersect(file('a'), file('b'))));

Re: Compare two text files, output line matches

Posted: Fri Dec 17, 2010 2:17 am
by Christopher
If you are on a Unix box you could exec a program like diff to do this.

Re: Compare two text files, output line matches

Posted: Fri Dec 17, 2010 10:19 am
by pickle
Christopher wrote:If you are on a Unix box you could exec a program like diff to do this.
+1

Re: Compare two text files, output line matches

Posted: Fri Dec 17, 2010 1:20 pm
by Engineerd
I am on a linux box/server.

Please let me know how I could best open file a, file b and write matchces to file c.

Thank you.

Re: Compare two text files, output line matches

Posted: Fri Dec 17, 2010 3:08 pm
by McInfo
Given "old" file A and "new" file B, the following command returns old lines as empty strings, new lines as empty strings, and unchanged lines as themselves. In other words, it returns only lines that exist in both A and B.* The string returned by diff through the standard output (STDOUT) is sent to file C.

Code: Select all

diff --old-line-format= --new-line-format= --unchanged-line-format=%L A B > C
* Note that, to be considered "unchanged", lines in file B must occur in the same order as they did in A. If file A has lines {a, b, c} and file B has lines {a, c, b}, file C will be given lines {a, c}. Line {b} is out of order, so it is considered "old" in file A and "new" in file B.

To execute the command from PHP, see Program Execution Functions.

For more information about diff, enter "man diff" at the command line or into your favorite search engine.

Re: Compare two text files, output line matches

Posted: Fri Dec 17, 2010 3:17 pm
by Engineerd
Here's how I did it kiddos.

Main file:

Code: Select all


<table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form action="multiple_upload_ac.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td><strong>OUTPUT MATCHES IN ANY TEXT, CSV, EXCEL DOCS</strong></td>
</tr>
<tr>
<td>Select file # 1
  <input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td>Select file # 2
  <input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
  <td align="center"><input type="submit" name="Submit" value="Upload" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<p>&nbsp;</p>

multiple_upload_ac.php

Code: Select all

<?php

$file1 = 'a.txt';
$file2 = 'b.txt';

//set where you want to store files
//in this example we keep file in folder upload
//$HTTP_POST_FILES['ufile']['name']; = upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0];
$path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1];

//rename files to a.txt and b.txt

$path1= "upload/a.txt";
$path2= "upload/b.txt";
//copy file to where you want to store file
copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2);

//$HTTP_POST_FILES['ufile']['name'] = file name
//$HTTP_POST_FILES['ufile']['size'] = file size
//$HTTP_POST_FILES['ufile']['type'] = type of file

///////////////////////////////////////////////////////

// Use this code to display the error or success.

$filesize1=$HTTP_POST_FILES['ufile']['size'][0];
$filesize2=$HTTP_POST_FILES['ufile']['size'][1];


if($filesize1 && $filesize2 != 0)
{	
	// Perform comparison and export functions
	$a=file($path1);
	$b=file($path2);
	file_put_contents("matching_entries.txt", implode(array_intersect($a, $b)));
	echo "<a href=\"matching_entries.txt\">Click Here To See Matching Entries</a>";
}

else {
echo "ERROR.....";
}

//////////////////////////////////////////////

// What files that have a problem? (if found)

if($filesize1==0) {
echo "There're something error in your first file";
echo "<BR />";
}

if($filesize2==0) {
echo "There're something error in your second file";
echo "<BR />";
}

?>

Re: Compare two text files, output line matches

Posted: Fri Dec 17, 2010 3:18 pm
by Engineerd
Just a note, the reason I did this is to make file manipulation that more flexible down-the-road.

Thanks all.

Re: Compare two text files, output line matches

Posted: Fri Dec 17, 2010 3:28 pm
by McInfo
$HTTP_POST_FILES is deprecated in favor of $_FILES.

Re: Compare two text files, output line matches

Posted: Fri Dec 17, 2010 3:44 pm
by Engineerd
Can you show me the best way to replace?

Re: Compare two text files, output line matches

Posted: Fri Dec 17, 2010 4:14 pm
by McInfo
I just mean that using $_FILES is preferred.

Re: Compare two text files, output line matches

Posted: Fri Dec 17, 2010 6:19 pm
by Engineerd
Note taken and will do moving forward!

Best