CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
comparing line by line from the text files. Please help

 



user1
User

Aug 12, 2005, 8:21 AM

Post #1 of 10 (2285 views)
comparing line by line from the text files. Please help Can't Post

I want to compare 2 textfiles (line by line) and print out the line which is different from the first file.

The files are in the below folders.

c:/Live/File12_Live.txt. (Base file)

c:/Server/File12.txt. (This file need to be tested against the base file to get the difference)



I am using the following script. Please help me with the below code
#!/usr/perl/bin

use strict;

use warnings;

opendir(DIR, "c:\\Live\\") or die "Can't open c:\\Results\\Server: $!";

my @files = grep (/^File[\w&=]+\_live.txt$/, readdir DIR);

close (DIR);

print "@files\n";

foreach $file (@files) {


open(<FILE1>,"$file");

@array1 = <FILE1>;

close(FILE1)


foreach $line(@array1) {

if ($line =~/^\w+/) { #if the line starts with the character,


$line++;

} #end of if


} # end of foreach line


} # end of foreach file


#open the 2nd file in the directory.




opendir(DIR, "c:\\Server\\") or die "Can't open c:\\Results\\Server: $!";

my @files = grep (/^File[\w&=]+\.txt$/, readdir DIR);

close (DIR);

print "@files\n";

foreach $file (@files) {


open(<FILE2>,"$file");

@array2 = <FILE1>;

close(FILE1)


foreach $line2(@array1) {

if ($line1 ne $line2) {

print "$line2\n";

} #end of if

} # end of foreach line


} #foreach file



Thanks for the help.


KevinR
Veteran


Aug 12, 2005, 2:32 PM

Post #2 of 10 (2282 views)
Re: [user1] comparing line by line from the text files. Please help [In reply to] Can't Post

look into the File::Compare module, it's a standard perl module.

http://perldoc.perl.org/File/Compare.html
-------------------------------------------------


user1
User

Aug 15, 2005, 8:04 AM

Post #3 of 10 (2274 views)
Re: [KevinR] comparing line by line from the text files. Please help [In reply to] Can't Post

I noticed that File::Compare module compares and gives the results as true or false. (I want to print out the lines in the file which are different. Is it still possible with the File::Compare module?


rork
User

Aug 15, 2005, 8:25 AM

Post #4 of 10 (2273 views)
Re: [user1] comparing line by line from the text files. Please help [In reply to] Can't Post

I don't think it's possible to do that, but I'm not sure.

Your code seems a bit of a mess to me, but here is my shot:

Code
open(FILE1, "<", $file1); 
my @file1 = <FILE1>;
close(FILE1);

open(FILE2, "<", $file2);
my @file2 = <FILE2>;
close(FILE2);

for (my $i = 0; $i <= $#file1; $i++) {
if ($file1[$i] ne $file2[$i]) {
print "$file2[$i]";
}
}

--
Don't reinvent the wheel, use it, abuse it or hack it.


KevinR
Veteran


Aug 15, 2005, 11:11 AM

Post #5 of 10 (2271 views)
Re: [user1] comparing line by line from the text files. Please help [In reply to] Can't Post


In Reply To
I noticed that File::Compare module compares and gives the results as true or false. (I want to print out the lines in the file which are different. Is it still possible with the File::Compare module?


Looks like it can:


Quote
File::Compare::compare_text does a line by line comparison of the two files. It stops as soon as a difference is detected. compare_text() accepts an optional third argument: This must be a CODE reference to a line comparison function, which returns 0 when both lines are considered equal.

-------------------------------------------------


user1
User

Aug 15, 2005, 2:13 PM

Post #6 of 10 (2269 views)
Re: [rork] comparing line by line from the text files. Please help [In reply to] Can't Post

I have used the code rork has sent but i am not able to execute the final piece of code where the loop gets the differences in both the files. Please help me with this.

The below code works fine except the last piece of code.

#!/usr/perl/bin

#use strict;

#use warnings;

#file on the server

opendir(DIR, "c:\\Server\\") or die "Can't open c:\\Server: $!";

my @files = grep (/^File[\w&=]+\.txt$/, readdir DIR);


foreach $file1 (@files) { /^(File[\w&=]+)\.txt$/;

open(FILE1, "<", ,"C:\\Server\\$file1") || die "can't open the file\n";

my @file1 = <FILE1>;

close(FILE1);

}


print "$file1\n"; #Outside this loop, the file doesn't get printed



# file on live


opendir(DIR, "c:\\Live\\") or die "Can't open c:\\Live: $!";

my @files2 = grep (/^File[\w&=]+\_live.txt$/, readdir DIR);


foreach $file2 (@files2) { /^(File[\w&=]+)\_live.txt$/;


#print "$file2\n";


open(FILE2, "<","C:\\Live\\$file2") || die "can't open the file\n";

my @file2 = <FILE2>;


close(FILE2);

}

The below piece of code doesn't work as i am not able to get the file names printed outside of the above for loops.

print "$file1\n"; #doesn't print

print "$file2\n"; #doesn't print

# i couldn't get the below code working.


for (my $i = 0; $i <= $#file1; $i++) {

if ($file1[$i] ne $file2[$i])

{

print "$file2[$i]";




} # end of if




} #end of for


rork
User

Aug 16, 2005, 1:00 AM

Post #7 of 10 (2267 views)
Re: [user1] comparing line by line from the text files. Please help [In reply to] Can't Post

You don't have the loops working right,

Code
opendir(DIR, "c:\\Server\\") or die "Can't open c:\\Server: $!"; 

my @files = grep (/^File[\w&=]+\.txt$/, readdir DIR);


foreach $file1 (@files) { /^(File[\w&=]+)\.txt$/;

open(FILE1, "<", ,"C:\\Server\\$file1") || die "can't open the file\n";

my @file1 = <FILE1>;

close(FILE1);

}

This wil open the directory c:\server\, load all files that are true to the regexp in @files, loop over @files and the content of that file are stored in a local @file1, which will not exist after the loop has finished.

I think you want to compare files in the directories that have the same filenames. Then do the compare in one loop.


Code
opendir(DIR, "c:\\server\\") or die "Can't open c:\\Server: $!"; 

my @files = grep (/^File[\w&=]+\.txt$/, readdir DIR);
foreach $file1 (@files) {
open(FILE1, "<", ,"C:\\Server\\$file1") || die "can't open the file\n";
my @file1 = <FILE1>;
close(FILE1);

#convert the filename form foo.txt to foo_life.txt
$file2 = $file1;
$file1 =~ s/\.txt$//i;
$file1 .= "_live.txt";

open(FILE2, "<","C:\\Live\\$file2") || die "can't open the file\n";
my @file2 = <FILE2>;
close(FILE2);

print "Comparing c:\\server\\$file1 to c:\\live\\$file2";
for (my $i = 0; $i <= $#file1; $i++) {
if ($file1[$i] ne $file2[$i]) {
print "$file2[$i]";
}
}
}


A loop always continues until the condition became false, then it will go on with the code after the loop.
--
Don't reinvent the wheel, use it, abuse it or hack it.


user1
User

Aug 16, 2005, 8:04 AM

Post #8 of 10 (2263 views)
Re: [rork] comparing line by line from the text files. Please help [In reply to] Can't Post

Thank you, Rork. I was able to run the code. It works fine.

I have one more problem when trying to find the differences between text files.

The format of the text file is something like (some lines are in the center and some are to the left)

Significant developments

Major reports

Add Favorites

Create Company Alert

business description.

---- End of the file

When 'Siginificant Developments' line (1) is missing, the script prints out the line 'Significant developments' (Works fine)

When 'Major reports' (line 2) is missing in the file, the script prints out all the lines from the line 'Major Reports' thinking that all the below lines are under line 2.

I manually edited the text file to have no left margins and the script works fine, printing the exact line 2 (Major Reports)

Is there a way to open the text file and have all the text put in without left margin or all in one margin for the script to understand as separate lines?

Thanks for your help again.


rork
User

Aug 16, 2005, 12:47 PM

Post #9 of 10 (2259 views)
Re: [user1] comparing line by line from the text files. Please help [In reply to] Can't Post

You can remove all whitspace at the beginning with the substition operator


Code
for (...) { 
$file1[$i] =~ s/^\s+//;
$file2[$i] =~ s/^\s+//;
if ($file1[$i] ne $file2[$i]) {
...
}
}


Small explanation about the regexp:
s/foo/bar/ = replace foo with bar
^ = start at the beginning of the string
\s = whitespace like space and tab
+ = 1 or more times
So it replace 1 or more whitspace at the beginning of the string with nothing.
--
Don't reinvent the wheel, use it, abuse it or hack it.


user1
User

Aug 16, 2005, 2:33 PM

Post #10 of 10 (2256 views)
Re: [rork] comparing line by line from the text files. Please help [In reply to] Can't Post

Thank you, Rork. It worked great!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives