473,399 Members | 2,774 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,399 software developers and data experts.

comparing two files

1
Hello,

I am not brilliant at perl (pretty rubbish) and would like to compare the contents of two files preferably with an output file of all the entries which are the same and also a count of how many entries are the same.

So far I have a bit of code which takes two files and saves them to arrays - not sure if thats the best approach, my code so far is below but as yet doesn't get down to the important part as I haven't a scooby as how to achieve this (have tried a few things and they have not worked)

I want to be able to compare text files with one "tag" name per line
(have so far been using simple files: File1.txt - A,B,C,D,E,F,G,H AND File2.txt - I,J,F,A,B,K,C,Y,Z)


Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/usr/bin/perl;
  3. use warnings;
  4. use strict;
  5.  
  6.  
  7. #global variables for the program
  8. my $file1 = "";
  9. my $file2 = "";
  10. my $line1 = "";
  11. my @tag1;
  12. my $line2 = "";
  13. my @tag2;
  14. my $same = 0;
  15. my $tag1 = "";
  16. my $tag2 = "";
  17.  
  18.  
  19. #get user to input a file
  20. print "Please type in a file name for comparison, and press enter\n";
  21. #save file to $file1 variable
  22. $file1 = <STDIN>;
  23. #remove new line from input
  24. chomp $file1;
  25. #open file, if file does not exist end 
  26. open (FILE1, "$file1") or die "Can't find file $file1";
  27.  
  28. #save file into array @tag1
  29. while ($line1 = <FILE1>){
  30. #read in each line from file and copy to $line, each new line saved to array @names
  31.     push (@tag1,$line1);
  32.     }
  33.  
  34. #Get user to enter an additional file to compare to
  35. print "Thank you\nPlease enter a second file for the comparison\n";
  36. #Save file to $file2
  37. $file2 = <STDIN>;
  38. #remove new line from input
  39. chomp $file2;
  40. #open file, if it does not exist end program
  41. open (FILE2, "$file2") or die "Can't find file $file2";
  42.  
  43. #save file into array @tag2
  44. while ($line2 = <FILE2>){
  45. #read in each line from file and copy to $line, each new line saved to array @names
  46.     push (@tag2,$line2);
  47.     }
  48.  
Thank you in advance!
Jan 27 '10 #1
2 2142
RedSon
5,000 Expert 4TB
Why reinvent the wheel?

http://search.cpan.org/search?query=diff&mode=all
Jan 27 '10 #2
numberwhun
3,509 Expert Mod 2GB
Or you could even use File::Compare. Either way, there are better ways to do it.

Also, if you post code, PLEASE use code tags!

Regards,

Jeff
Jan 28 '10 #3

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

Similar topics

11
by: Dan Stromberg | last post by:
We will soon have 3 copies, for testing purposes, of what should be about 4.5 terrabytes of data. Rather than cmp'ing twice, to verify data integrity, I was thinking we could speed up the...
8
by: Corno | last post by:
Hi all, Does anybody know a command line tool (linux or windows) that can compare 2 XML files. There's only 1 extra requirement that makes it a bit more complicated: the order of attributes or...
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...
4
by: ddd | last post by:
I am trying to build a diff tool that allows me to compare two HTML files. I am looking for resources on how to achive this. The main problem is that I do not want to simply highlight the line of...
5
by: Peteroid | last post by:
I realize this might not be the correct newsgroup, but since I have a general question I need answering in the context of doing a managed VC++.NET Managed application, this is as good a place as...
0
by: richardkreidl | last post by:
I have the following hash script that I use to compare two text files. 'Class Public Class FileComparison Public Class FileComparisonException Public Enum ExceptionType U 'Unknown A 'Add...
3
by: s99999999s2003 | last post by:
hi i wrote some code to compare 2 files. One is the base file, the other file i got from somewhere. I need to compare this file against the base, eg base file abc def ghi eg another file
3
by: system55 | last post by:
are there any commands that can compare two files? Comparing in the sense that the two files are alike... Because I wrote a program that makes a backup file of another file... Just want to know if...
0
by: Gabriel Genellina | last post by:
En Thu, 15 May 2008 06:02:29 -0300, Beema shafreen <beema.shafreen@gmail.comescribió: *three* columns? You menction only two of them. Do you have to syncrhonize both listings? That is,...
1
by: Avi1 | last post by:
Hi, I got the code (from the internet)for comparing two files and showing the difference in contents.Now,I tried the same code for two files written in japanese language(kanji).If I save the two...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.