To compare two files and search for a word in both files 
Author Message
 To compare two files and search for a word in both files

I am working on one perl script (on windows).

I have two files- the first one with hunded lines and
the second one with 5 lines (5 words (one per line),
actaully). I have to compare the contents of the
second file with those of first file.

If any of the 5 words in the second file is not found
in the first file of 100 lines, I should print that
that specific word is not found in the first file. Say, if 3 out of 5
words (from the second file) are not found in the first file, it
should print all of them as the output.

Example:

first file contents:
---------------------------------------------
john_test    C:\\Johntest\\Local
smith_test   D:\\Test
somers_test  C:\\TEst\test1.cpp
Bob_test     D:\\Hithere
Jimmy_test   C:\\Simple\\simple.cpp
Rick_test    C:\\Simple\\hellp.h
...............................
............................. (up to qround 100 lines this way)
--------------------------------------------

Second file contents (one word per line):
------------------------------
somer_test
Bob_test
danny_test
smith_test
guru_test
student_test
------------------------------

Now, the output should be as follows:

---------------------------------------------------
The below 3 files are missing (in alpha series):

1. danny_test
2. guru_test
3. student_test
-------------------------------------------------

I tried to code, but it was not giving me the desired
result - some thing wrong in the foreach and while
loops.

Would one of you kindly come up with the code for this
scenario?

Thanks in advances,
John



Mon, 04 Jul 2005 01:32:20 GMT  
 To compare two files and search for a word in both files

Quote:

> I have two files- the first one with hunded lines and
> the second one with 5 lines (5 words (one per line),
> actaully). I have to compare the contents of the
> second file with those of first file.

> If any of the 5 words in the second file is not found
> in the first file of 100 lines, I should print that
> that specific word is not found in the first file. Say, if 3 out of 5
> words (from the second file) are not found in the first file, it
> should print all of them as the output.

This sounds rather simple.
[Example snipped]

Quote:
> I tried to code, but it was not giving me the desired
> result - some thing wrong in the foreach and while
> loops.

So, why don't you show us your code such that we can have a look at it and
find out what went wrong?

Quote:
> Would one of you kindly come up with the code for this
> scenario?

Nope, you will learn much more by improving your own code instead of asking
for a finished script.
Which by the way is not the purpose of this NG. If you want a script written
then go to jobs.offered.

jue



Mon, 04 Jul 2005 11:22:54 GMT  
 To compare two files and search for a word in both files

Quote:

> I am working on one perl script (on windows).

> I have two files- the first one with hunded lines and
> the second one with 5 lines (5 words (one per line),
> actaully). I have to compare the contents of the
> second file with those of first file.

> If any of the 5 words in the second file is not found
> in the first file of 100 lines, I should print that
> that specific word is not found in the first file. Say, if 3 out of 5
> words (from the second file) are not found in the first file, it
> should print all of them as the output.
[snip]
> I tried to code, but it was not giving me the desired
> result - some thing wrong in the foreach and while
> loops.

Try showing us the code.

Quote:
> Would one of you kindly come up with the code for this
> scenario?

Fine:
# This code is Copyright (c) 2003 Bryan Donlan
# This code is for demonstrative purposes only
# You may not use this in your homework, or for
# any commercial uses.

open WORDS,"<$fileone" or die "Opening $fileone: $!\n";
open SEARCH,"<$filetwo" or die "Opening $filetwo: $!\n";


my %words;

        chomp $word;
        $words{$word} = 1;

Quote:
}

while(my $line = <SEARCH>){


                if(index($line,$word) != -1){
                        delete $words{$word};
                }
        }

Quote:
}

foreach $word (keys %words){
        print "$word not found\n";

Quote:
}

close WORDS;
close SEARCH;

--
Replace spamtrap with bd to reply.
A shapely CATHOLIC SCHOOLGIRL is FIDGETING inside my costume..



Tue, 05 Jul 2005 23:49:11 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Had trouble comparing 2 files using File::Compare

2. comparing two chinese words

3. compare MS Word files

4. perl module to compare two files?

5. compare two files

6. compare two files - help!!

7. Help comparing two files using hashes

8. comparing the time stamp of two files

9. How to compare two files?

10. To compare the content in two files..

11. compare two files

12. File I/O: want to compare two or more lines @ once

 

 
Powered by phpBB® Forum Software