freelanceprogrammers.org Forum Index » Perl
updating file
Joined: 05 Mar 2005
Posts: 3
updating file
Hi,
I want to compare two files and print out the
lines which are common to both files
There is utility in unix called "comm"
comm - select or reject lines common to two files
any idea to do this kind of job using perl
Thanks
pk
Joined: 21 May 2004
Posts: 44
updating file
>>>>> "pkkjb" == pkkjb <pkkjb@...> writes:
pkkjb> I want to compare two files and print out the
pkkjb> lines which are common to both files
pkkjb> There is utility in unix called "comm"
pkkjb> comm - select or reject lines common to two files
pkkjb> any idea to do this kind of job using perl
comm requires that the lines be in alphabetical order.
You can do a more generic job with Perl by printing the lines that
appear in *any* order. Simply read all of the first file, creating a
hash entry for every line of the file. Then read the second file, and
if the line is in the hash, it`s also in the first file.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Joined: 21 Apr 2005
Posts: 3
updating file
Hai Pk,
Although there are lot of logic to compare
two file, i have come up with simple logic. PLease see
that this program helps u..
#!/usr/local/bin/perl
use strict;
use warnings;
open(FILE1,"<$ARGV[0]") || die "Cant open file1";
open(FILE2,"<$ARGV[1]") || die "Cant open file2";
open(FILE3,">$ARGV[2]") || die "Cant open file3";
while(<FILE1>) {
my $line1 = $_;
while(<FILE2>) {
my $line2 = $_;
if($line2 eq $line1) {
print FILE3 $.." ";
print FILE3 $line2;
}
}
}
Regards
Prasanna.K
--- pkkjb <pkkjb@...> wrote:
> Hi,
>
> I want to compare two files and print out the
> lines which are common to both files
>
> There is utility in unix called "comm"
>
> comm - select or reject lines common to two files
>
> any idea to do this kind of job using perl
>
> Thanks
> pk
>
>
>
>
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
Joined: 21 May 2004
Posts: 44
updating file
>>>>> "k" == k prasanna <kprasanna_79@...> writes:
k> Hai Pk,
k> Although there are lot of logic to compare
k> two file, i have come up with simple logic. PLease see
k> that this program helps u..
k> #!/usr/local/bin/perl
k> use strict;
k> use warnings;
k> open(FILE1,"<$ARGV[0]") || die "Cant open file1";
k> open(FILE2,"<$ARGV[1]") || die "Cant open file2";
k> open(FILE3,">$ARGV[2]") || die "Cant open file3";
k> while(<FILE1>) {
k> my $line1 = $_;
k> while(<FILE2>) {
k> my $line2 = $_;
k> if($line2 eq $line1) {
k> print FILE3 $.." ";
k> print FILE3 $line2;
k> }
k> }
k> }
And AGAIN with the broken logic! PLEASE STOP POSTING.
Your program reads the first line of the first file, and compares
all the lines of the second file to it, and prints the line number
of the line that compares if any.
THEN it simply reads the remainder of the first file. Since the second
file is already at EOF, the inner loop is never executed!
THUS, it will correctly locate the first line in the second file,
but ignore any remaining lines from the file.
PLEASE STOP POSTING YOUR UNTESTED CODE.
Four times in the past four days, you`ve given broken code or
misleading advice.
You`re just wasting the reader`s time, and my time for having to
point out that your code is broken.
You appear to be both a beginner to programming (judging by the style
of the code above) and clearly a beginner to Perl (by your general
answers). Good time to sit back and *learn*, not *teach*.
And to the rest of the group, please note this name, and ignore
anything he says for now. {sigh}
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Joined: 22 May 2005
Posts: 3
updating file
Bonjour,
Ne serait-il pas plus pédagogique de poster une bonne solution ?
merci d`avance,
Georges
-------------------------------------------------------------
NetCourrier, votre bureau virtuel sur Internet : Mail, Agenda, Clubs, Toolbar...
Web/Wap : www.netcourrier.com
Téléphone/Fax : 08 92 69 00 21 (0,34 € TTC/min)
Minitel: 3615 NETCOURRIER (0,16 € TTC/min)
Joined: 05 Mar 2005
Posts: 3
updating file
Thanks for your dirction.
Here is what I did:
!/home/tools/bin/perl
#script to print out [common or unique] lines from
file2 compared to file1
die "USSAGE : $0 [file1] [file2] [outFile]
" if @ARGV
!= 4;
die "ERORO
" if ($ARGV[3] ne "diff" && $ARGV[3] ne
"common");
open(file1, "$ARGV[0]") or die "Could not find
$ARGV[0]
";
open(file2, "$ARGV[1]") or die "Could not find
$ARGV[1]
";
open(OUT, ">$ARGV[2]") or "die could not open
$ARGV[2]
";
$mode = $ARGV[3];
foreach $line (<file1>){
$LineFileOne{$line}++;
}
#diff
if($mode eq "diff"){
system "touch $ARGV[2]";
foreach $line (<file2>){
next if($LineFileOne{$line});
print OUT $line;
}
#common
}elsif($mode eq "common"){
system "touch $ARGV[3]";
foreach $line (<file2>){
if($LineFileOne{$line}){
print OUT $line};
}
}
close file1;
close file2;
--- "Randal L. Schwartz" <merlyn@...>
wrote:
> >>>>> "pkkjb" == pkkjb <pkkjb@...> writes:
>
> pkkjb> I want to compare two files and print out the
> pkkjb> lines which are common to both files
>
> pkkjb> There is utility in unix called "comm"
>
> pkkjb> comm - select or reject lines common to two
> files
>
> pkkjb> any idea to do this kind of job using perl
>
> comm requires that the lines be in alphabetical
> order.
>
> You can do a more generic job with Perl by printing
> the lines that
> appear in *any* order. Simply read all of the first
> file, creating a
> hash entry for every line of the file. Then read
> the second file, and
> if the line is in the hash, it`s also in the first
> file.
>
> --
> Randal L. Schwartz - Stonehenge Consulting Services,
> Inc. - +1 503 777 0095
> <merlyn@...>
> <URL:http://www.stonehenge.com/merlyn/>
> Perl/Unix/security consulting, Technical writing,
> Comedy, etc. etc.
> See PerlTraining.Stonehenge.com for onsite and
> open-enrollment Perl training!
>
All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help
China Wholesale - Electronics Products
Character Studio - Tutorials and Help







