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: - sample:
-
file 1
-
-
a,"1",g,c
-
-
file 2
-
-
a,"1",c
-
-
after comparison i want file 3 to hold the missing content that is
-
-
file 3
-
g
will be thankful to whatever help i get
thanks
mano
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
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.
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
hi all
i just tried comparing the two files with the following code.. - use File::Compare;
-
-
if (compare("data(old).txt","data.txt") == 0) {
-
print "They're equal\n";
-
} else {
-
open(CHECK,">deletes.txt");
-
print "They are not equal";
-
}
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
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: -
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
-
If you have a copy of Learning Perl, this type of comparison is also explained briefly in there as well.
Regards,
Jeff
hi
i tired that lines to compare and copy the contents to newfiles but it shows tan error saying - bareword found where oprerator expected near the lines
-
-
"\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
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
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 - '$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
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
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
numberwhun 3,509
Recognized Expert Moderator Specialist
Ok, let me try to explain. The following code: -
'$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
-
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
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
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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....
|
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...
|
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...
|
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
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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: 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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |