Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file compareson

Status
Not open for further replies.
Joined
Apr 30, 2003
Messages
56
Location
US
I have two files A and B. The content of file A is as follows:
aaa
bbb
ccc
111
222
333

The content of file B is as follows:
111
222
333
ddd
444
eee

There are three lines in file B match those of file A. I need to write a scipt that compares those two files. If there is a match line, then I need to remove those line from file B. How would I be able to do that? Please help. Thanks.
 
is this homework? Was your other question also homework?
 
No, this is not home work. I use this file name and content just for example purpose. The real file I can not show since it is confidential. I am new to the job.
 
for i in `cat filea`
do
grep -v $i fileb > filec
mv filec fileb
done
 
Also, for anything over a few hundred lines per file, this is going to be hugely inefficient, as file b has to be read and compared once for every line in file a.

Eileen - what do you expect to happen if there are duplicate lines in file b? Does the order of the contents of fileb need to be preserved?
Code:
#!/usr/bin/perl
use strict;
use warnings;
my %filea;

open(FILEA, "filea.txt") or die $!;

while (<FILEA>) {
    $filea{$_}++;
}

open(FILEB, "fileb.txt") or die $!;

while (<FILEB>) {
    print $_ unless exists $filea{$_};
}
At the moment this print the results. You can either redirect the output to a file, or change it to write to a file yourself.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::PerlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top