473,505 Members | 13,982 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Difference between two files

10 New Member
hi all..

i have just now started working in perl... i have a requirment where in i need to compare two text files.. and need to print the contents that are not present in file 2 during comparison into a third file.. i am able to compare files but dont know how to move the missing contents into new file.. can anyone kindly help me in doing it.. to make things clear all i want to do is:
Expand|Select|Wrap|Line Numbers
  1. sample:
  2. file 1
  3.  
  4. a,"1",g,c
  5.  
  6. file 2
  7.  
  8. a,"1",c
  9.  
  10. after comparison i want  file 3 to hold the missing content that is 
  11.  
  12. file 3
  13.  g
will be thankful to whatever help i get

thanks
mano
Aug 29 '07 #1
13 12989
numberwhun
3,509 Recognized Expert Moderator Specialist
Well, this sounds like it is part of a class that you are taking. Is that correct? Either way, we can assist, but want to see what you have tried thus far. Please post your code.

I can say this, if you have "Learning Perl, 4th Edition", there is a piece in there that goes over how to compare two files and do exactly what you want.

Also, if you don't have a coy of Learning Perl, then read this blog as it is pretty much the same type of comparison as in the book.

Regards,

Jeff
Aug 29 '07 #2
rellaboyina
55 New Member
Can I assume that the file file1 always contain more elements than those of file2 or whether the file2 can contain more elements than those of file1?

Please be clear in that as the code differs in both the cases.
Aug 29 '07 #3
numberwhun
3,509 Recognized Expert Moderator Specialist
It should not matter if one file has more elements than the other. What the code does is take the first file and put all the unique lines into the output file, disregarding any duplicates. Then, it takes the second file and adds its lines that are unique, again, discarding the duplicates.

Regards,

Jeff
Aug 29 '07 #4
manoprasanna
10 New Member
hi all

i just tried comparing the two files with the following code..
Expand|Select|Wrap|Line Numbers
  1. use File::Compare;
  2.  
  3. if (compare("data(old).txt","data.txt") == 0) {
  4.     print "They're equal\n";
  5. } else {
  6.     open(CHECK,">deletes.txt");
  7.     print "They are not equal";
  8. }
now what happens is it just says they are not equal... since file 1 has more values than file2.. now i want those missing values in file2 to be separetely printed on to file 3. how can i do that. can anyone help me doing it..


thanks to whatever help being offered

thanks

mano
Aug 30 '07 #5
numberwhun
3,509 Recognized Expert Moderator Specialist
If you read the link that I provided you, then you will see that you will be able to combine your two files into one file without using the File::Compare module. The following Perl one liner is from that link and is also explained there as to how it works:

Expand|Select|Wrap|Line Numbers
  1. perl -e '$count=0; while (<>) {if (! ($var{$_}++)) {print $_; $count++;}} warn "\n\nRead $. lines.\nTook union and removed duplicates, yielding $count lines.\n"' ./file1 ./file2.txt > ./combined.txt
  2.  
If you have a copy of Learning Perl, this type of comparison is also explained briefly in there as well.

Regards,

Jeff
Aug 30 '07 #6
manoprasanna
10 New Member
hi

i tired that lines to compare and copy the contents to newfiles but it shows tan error saying

Expand|Select|Wrap|Line Numbers
  1. bareword found where oprerator expected near the lines
  2.  
  3. "\n\nRead $. lines.\nTook union and removed duplicates, yielding $count lines.\n"' ./file1 ./file2.txt > ./combined.txt
i dont how to proceed can u help in telling what could be done

expecting a reply at the earliest
thanks

mano
Sep 1 '07 #7
numberwhun
3,509 Recognized Expert Moderator Specialist
Are you executing this as a perl one liner or are you trying to incorporate it into a script? Either way, please post the code as you have entered it.

Regards,

Jeff
Sep 1 '07 #8
manoprasanna
10 New Member
hi

i need to put this code inside another script.but this is going to be in separate function so i planned to use it separetly only till now.. the code which i coded is

Expand|Select|Wrap|Line Numbers
  1. '$count=0; while (<>) {if (! ($var{$_}++)) {print $_; $count++;}} warn "\n\nRead $. lines.\nTook union and removed duplicates, yielding $count lines.\n"'>./data.>/data1.txt > ./combined.txt
where data is the name of file1 and data1 is the second file name. when i executed this script it showed that error which i had mentioned above..

kindly help me

thanks
mano
Sep 2 '07 #9
numberwhun
3,509 Recognized Expert Moderator Specialist
Ok, I had a feeling that that is what you were trying to do and just wanted to confirm it.

You cannot use the one liners format in your script the way it is. You are going to have to code it into your script, even if it is a separate function.

The reason is, because the way the one liner is, it has single quotes to contain it. Also, you would want to oppen the different input files for reading from and then open the output file for writing to(with a filehandle).

Since you have the necessary code to do the de-duplicadtion, why don't you go ahead and throw some code together to do this, and if you get stuck, post all of the code back here and we will help you. I am not trying to be cruel, but if Ii write it for you, how will you learn? Removing the enclosing single quotes is a good start though, in transforming this code.

Regards,

Jeff
Sep 2 '07 #10
manoprasanna
10 New Member
hi jeff

Am sorry i am not able to understand what you are trying to explain.. i have just now started working in perl so dont have much exposure to it..so only am asking.. i too want to learn it myself but just expect some sort of guidence. if u can explain me what sholud be done in a lighter manner will surely try things out myself

thanks

mano
Sep 2 '07 #11
numberwhun
3,509 Recognized Expert Moderator Specialist
Ok, let me try to explain. The following code:

Expand|Select|Wrap|Line Numbers
  1. '$count=0; while (<>) {if (! ($var{$_}++)) {print $_; $count++;}} warn "\n\nRead $. lines.\nTook union and removed duplicates, yielding $count lines.\n"'>./data.>/data1.txt > ./combined.txt
  2.  
the way it is shown here, is meant to be executed on the command line, due to it being surrounded by the single quotes = ' and '.

One thing I noticed is that the original code listed the files (ie: file1.txt and file2.txt) one after the other after the statements. What you have done is add a ">" before each file name. This, in shell terms is a redirection OUT to those files. The way it was originally written, it would take each file as input and the last file, preceded by a ">" was what was being outputted to.

I will try and produce something that will take a list of files and process them each into a single file, removing duplicates, but it is going to take a little while as I have some things going on right now.

Regards,

Jeff
Sep 2 '07 #12
manoprasanna
10 New Member
hi jeff

i tired removing that redirection symbol that i had inserted then also it showing the same error..hope u can provide me with an example so that i might be able to correlate it with mine....

expecting some kind of help.. sorry for the trouble caused

thanks
mano
Sep 2 '07 #13
numberwhun
3,509 Recognized Expert Moderator Specialist
The error you are getting about the bare word means that you have a word that is not a recognized command used in your code. If you have the redirection and the files in there, then that is your problem. In perl code, you cannot put a redirect to a file directly. You have to typically print to a file handle. All of the code within the single quotes is valid, but that's about the extent of it. You have to code to use the files in your script.

Like I said, it will take a little while with things going on, but I will try and get you an example.

Regards,

Jeff
Sep 2 '07 #14

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

Similar topics

4
9995
by: Nimmi Srivastav | last post by:
Once and for all can someone kindly tell me the difference between C and C++ linkage. I thought I understood it till someone showed me the other day that C functions, that would ordinarily require...
0
3224
by: wooks | last post by:
<?xml version="1.0" ?> - <xsd:schema targetNamespace="urn:faster:userlogin" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:faster:userlogin"> <xsd:include...
12
2029
by: MikeT | last post by:
I have a page that produces little thumbnails of the 3D models it finds in a specified directory (and iterates down through any sub directories). It basically scans each directory for 3D Studio...
3
11598
by: John Bailo | last post by:
With OS400, all files are objects that can be accessed relationally. When I view the file system from Navigator (windows client), I see there is the Database and SQL tables, then there is the...
2
5208
by: Hai Ly Hoang [MT00KSTN] | last post by:
CString.FormatMessage and CString.Format has the same parameters and they seems to have to outcome. However, they coexist !. There must be some subtle different between they. Can you tell me the...
4
1449
by: Satish | last post by:
what is the difference between persistet DLL(ASP.NET Temp Files) and one which is IIS/Webapplication/ BIN Folder. can anyone tell what exactly happens for the first request for the above senario....
1
19808
by: bharathreddy | last post by:
This Article gives an introduction to VSTS Team Foundation & fundamental difference between Visual Source Safe (VSS) and VSTS Team Foundation. Team Foundation is a set of tools and technologies...
3
1810
nikpreek
by: nikpreek | last post by:
Hi All, I know its very basic question and event I know the answer for this. But I haven't found any proof of this. Is there any difference between thse queries (given below at end of post)? (In...
34
19781
by: Mark Sullivan | last post by:
What is the difference between the extensions *.hxx and just *.h for header files ? Can they co-exist? Mark
5
2991
by: daveh551 | last post by:
What, from a high level point of view, is the difference (in Visual Studio 2005) between Website (accessed with Open Website or Create Website from the StartPage) that is an ASP.NET Website, and a...
0
7213
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
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7298
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
7366
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...
1
7017
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
5610
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,...
1
5026
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...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1526
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.