?
Solved

remove duplicate lines found in another file

Posted on 2012-08-17
3
Medium Priority
?
331 Views
Last Modified: 2012-08-17
Hi experts,

I need to remove from a.txt every line found in b.txt (case sensitive)
The files contain one word on each line.

Thanks.
0
Comment
Question by:gloriaewold41
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 2000 total points
ID: 38305172
<?php
$test = true;
$afile = file("a.file");
$bfile = file("b.file");
$diff = array_diff($afile,$bfile);

if($test)
{
  print_r($diff);
}
else
{
  file_put_contents("a.file",$diff);
}
?>
0
 
LVL 35

Assisted Solution

by:gr8gonzo
gr8gonzo earned 2000 total points
ID: 38305189
That should work for most general needs. If a and b are huge files, then you might run into memory issues, and may need to either increase the memory limit or use a more complex and slower way to do this.

Set $test to false when you have reviewed the results and are satisfied that it meets your needs. It should overwrite "a.file" with the results.
0
 

Author Closing Comment

by:gloriaewold41
ID: 38305219
Works great. Thanks.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question