473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Comparing Lines and Printing the Ones that are Different

I am trying to compare two text files line by line and print out the lines that are different. This is the code that i have, but it's not working. Any suggestions?


[HTML]
open (REPORT_FILE, ">>$reportfile");
open (FILE, "<$file");
open (COPIED_FILE, "<$copiedfile");

my @outlines;

foreach(<COPIED_FILE>)
{
$y = 0;
$correct_line = $_;

seek(FILE,0,0);

foreach(<FILE>)
{
$results_line = $_;

if($correct_line eq $results_line)
{
$y = 1;
last;
}
}

if($y != 1)
{
print REPORT_FILE "The following lines do not match for :\n $correct_line (Known Message)\n $results_line (JWARN Message)\n";
}
}

close FILE;
close COPIED_FILE;[/HTML]

Thank you,
Terra
Oct 1 '07 #1
3 1428
KevinADC
4,059 Expert 2GB
Are you doing a line by line comparison or comparing all lines in both files wih all the lines in each file? If the latter, build a hash of the first file and loop through the second file and see if the hash keys exists, if the former just compare each line in sequence and push the not equal lines into an array to print later.
Oct 1 '07 #2
I am doing a line by line comparison. So i will try what you suggested and compare each line in sequence and push the not equal lines into an array to print later. But i thought that's what i was trying to do in the code that i provided...

Thanks,
Terra
Oct 2 '07 #3
KevinADC
4,059 Expert 2GB
untested code, should get you started:

Expand|Select|Wrap|Line Numbers
  1. open (FILE, "<$file"); 
  2. open (COPIED_FILE, "<$copiedfile"); 
  3.  
  4. my @outlines;
  5.  
  6. while (my $copied = <COPIED_FILE>) {
  7.    chomp($copied);
  8.    defined $line = <FILE> or die "end of file";
  9.    chomp($line);
  10.    push @outlines, ($copied,$line)  if ($copied ne $line);
  11. }
  12.  
  13. open (REPORT_FILE, ">>$reportfile");
  14. foreach $line (@outlines) {
  15.    print REPORT_FILE "The following lines do not match for :\n $outlines[0] (Known Message)\n  $outlines[1](JWARN Message)\n";
  16. }
  17.  
  18. close FILE;
  19. close COPIED_FILE;
  20. close REPORT_FILE;
  21.  
Oct 2 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: André Hänsel | last post by:
Hi! I compared the printed results of two of my pages. One is a very old page, "styled" almost without any CSS. The other one is a modern page, styled completely in CSS and styled for printing,...
1
by: BJS | last post by:
Sorry for the cross-posting, but based on the number of people I have seen ask for a solution to this problem, I hope by cross-posting this, that it will help a lot of people out of a common...
18
by: HerrLucifer | last post by:
I am each day becoming more and more addicted to the new .Net framework because of its cool RAD facilities. However, performance and speed is extremely important for my application development. I...
41
by: Odd-R. | last post by:
I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and...
8
by: Frost | last post by:
Hi All, I am a newbie i have written a c program on unix for line by line comparison for two files now could some one help on how i could do word by word comparison in case both lines have the...
20
by: Bill Pursell | last post by:
This question involves code relying on mmap, and thus is not maximally portable. Undoubtedly, many will complain that my question is not topical... I have two pointers, the first of which is...
7
by: peraklo | last post by:
Hello, there is another problem i am facing. i have a text file which is about 15000 lines big. i have to cut the last 27 lines from that file and create a new text file that contans those 27...
27
by: Thomas Kowalski | last post by:
Hi everyone, To determine equality of two doubles a and b the following is often done: bool isEqual ( double a, double b ) { return ( fabs (a-b) < THRESHOLD ); } But this a approach usually...
18
by: Carramba | last post by:
Hi! is there a better/faster way to compare mantissas of to real number then in following code? #include <stdio.h> #include <stdlib.h> int main(void) { float a,b; int test;
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.