Compare two text files, output line matches
Moderator: General Moderators
Compare two text files, output line matches
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.
Any help?
Love you guys btw.
Re: Compare two text files, output line matches
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'))));- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Compare two text files, output line matches
If you are on a Unix box you could exec a program like diff to do this.
(#10850)
Re: Compare two text files, output line matches
+1Christopher wrote:If you are on a Unix box you could exec a program like diff to do this.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Re: Compare two text files, output line matches
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.
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
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.
* 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.
Code: Select all
diff --old-line-format= --new-line-format= --unchanged-line-format=%L A B > CTo 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
Here's how I did it kiddos.
Main file:
multiple_upload_ac.php
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> </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
Just a note, the reason I did this is to make file manipulation that more flexible down-the-road.
Thanks all.
Thanks all.
Re: Compare two text files, output line matches
$HTTP_POST_FILES is deprecated in favor of $_FILES.
Re: Compare two text files, output line matches
Can you show me the best way to replace?
Re: Compare two text files, output line matches
I just mean that using $_FILES is preferred.
Re: Compare two text files, output line matches
Note taken and will do moving forward!
Best
Best