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

Perl Script

how to compare two file and update the second file with comparing to first file?
plz help
Feb 11 '08 #1
8 1508
nithinpes
410 Expert 256MB
how to compare two file and update the second file with comparing to first file?
plz help
Kindly provide more clarification on your query, what you want to update in second file? Also, post code/part of code that you tried to execute so that someone can tell you where you went wrong.
To compare contents of two files, you can assign file-handles to two arrays and compare elements of these arrays.
Feb 11 '08 #2
Kindly provide more clarification on your query, what you want to update in second file? Also, post code/part of code that you tried to execute so that someone can tell you where you went wrong.
To compare contents of two files, you can assign file-handles to two arrays and compare elements of these arrays.
hi i am new to perl. i need to compare to files and make an update if any changes happen in second file, i need to update it in first file. more probably two files are in same format. i use these following code to remove line which are not in etcshadow and present in another file. but i need to update if any word changes in etcshadow and to the new file.
Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/perl
  2.  
  3. open (etcshadow, $ARGV[0]) || die "cannot open: $!";
  4. open (check, $ARGV[1]) || die "cannot open: $!";
  5. $newfile="newcheckedfile";
  6. open (TEXT,">$newfile") || die "cannot open: $!";
  7. @personal=<check>;
  8. @etcfile=<etcshadow>;
  9. $i=0;
  10.  
  11. foreach $personal(@personal)
  12. {
  13. @persinfo = split(/:/, $personal);
  14.  
  15. foreach $etcfile(@etcfile)
  16. {
  17. @etcinfo=split(/:/,$etcfile);
  18. if($etcinfo[0] =~ m/^$persinfo[0]$/)
  19. {
  20. $i=$i+1;
  21. }
  22. }
  23. if($i!=0)
  24. {
  25. print TEXT $personal;
  26. }
  27. $i=0;
  28. }
  29. close(TEXT);
  30. rename("newcheckedfile",$ARGV[1]);
Feb 11 '08 #3
nithinpes
410 Expert 256MB
In your script, you are comparing first word of each line . In case, the first word is unique in a file, without any repetition, a small modification in your script should work:

Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/perl
  2.  
  3. open (etcshadow, $ARGV[0]) || die "cannot open: $!";
  4. open (check, $ARGV[1]) || die "cannot open: $!";
  5. $newfile="newcheckedfile";
  6. open (TEXT,">$newfile") || die "cannot open: $!";
  7. @personal=<check>;
  8. @etcfile=<etcshadow>;
  9. $i=0;
  10.  
  11. foreach $personal(@personal)
  12. {
  13. @persinfo = split(/:/, $personal);
  14.  
  15. foreach $etcfile(@etcfile)
  16. {
  17. @etcinfo=split(/:/,$etcfile);
  18. if($etcinfo[0] =~ m/^$persinfo[0]$/)
  19. {
  20. $i=$i+1;
  21. }
  22. if($i!=0)
  23. {
  24. print TEXT $etcfile;
  25. last;
  26. }
  27. }
  28. $i=0;
  29. }
  30. close(TEXT);
  31. rename("newcheckedfile",$ARGV[1]);
  32.  
  33.  
Instead of printing line from second file, I am printing from first file on the condition that it is present in second one. If this is not the case, compare entire string instead of first word.
Feb 11 '08 #4
In your script, you are comparing first word of each line . In case, the first word is unique in a file, without any repetition, a small modification in your script should work:

Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/perl
  2.  
  3. open (etcshadow, $ARGV[0]) || die "cannot open: $!";
  4. open (check, $ARGV[1]) || die "cannot open: $!";
  5. $newfile="newcheckedfile";
  6. open (TEXT,">$newfile") || die "cannot open: $!";
  7. @personal=<check>;
  8. @etcfile=<etcshadow>;
  9. $i=0;
  10.  
  11. foreach $personal(@personal)
  12. {
  13. @persinfo = split(/:/, $personal);
  14.  
  15. foreach $etcfile(@etcfile)
  16. {
  17. @etcinfo=split(/:/,$etcfile);
  18. if($etcinfo[0] =~ m/^$persinfo[0]$/)
  19. {
  20. $i=$i+1;
  21. }
  22. if($i!=0)
  23. {
  24. print TEXT $etcfile;
  25. last;
  26. }
  27. }
  28. $i=0;
  29. }
  30. close(TEXT);
  31. rename("newcheckedfile",$ARGV[1]);
  32.  
  33.  
Instead of printing line from second file, I am printing from first file on the condition that it is present in second one. If this is not the case, compare entire string instead of first word.
thanx i got it.but i need to change like this. here i need to update like this
for example original file content like this

karthick:$1$$RTqB41oJHFmaKtHKTQzFh1:12922::99999: :::

and the second file content like

karthick:$1$HVDD6786KGDjbscscc.a0

here while comparing these two file content i need to update only upto
2nd colon starts. ie name followed by ':' and then followed by encrypted
letters ends before 2nd colon. it should update if name(karthick) is same in both file content.
ie i would get like this.
karthick:$1$$RTqB41oJHFmaKtHKTQzFh1
Feb 13 '08 #5
nithinpes
410 Expert 256MB
In the case you have to update only first two colon separated fields in each line, you just modify the block where you are printing to new file in the previous script that I posted like below:

Expand|Select|Wrap|Line Numbers
  1. if($i != 0)
  2. {
  3.   @tempinfo=@persinfo; 
  4.  ## replace first two elements with that in @etcinfo
  5.   splice(@tempinfo,0,2,($etcinfo[0],$etcinfo[1])); 
  6.   $newinfo= join( ':' , @tempinfo);
  7.   print TEXT $newinfo;
  8.   last;
  9. }
  10.  
Feb 13 '08 #6
nithinpes
410 Expert 256MB
Just for the matter of simplicity, you can use the splice function like this:

Expand|Select|Wrap|Line Numbers
  1.   splice(@tempinfo,0,2,@etcinfo[0,1]); 
  2.  
  3.  
Feb 13 '08 #7
Kelicula
176 Expert 100+
I have found it very useful to "tie" a file in this situation.
That way you can use most of the regular functions that associate with arrays.

See Here

Once the files have been tied to the arrays, you can use: pop, shift, splice, unshift, push etc...

This also reduces overhead because perl doesn't hold the actual file in memory.
It holds special pointers. All changes made to the array are made to the file.

The code could look something like this:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use Tie::File;
  7.  
  8. my @holder1;
  9. my @holder2;
  10. my $i = 0;
  11.  
  12. tie my @etcshad, 'Tie::File', "$ARGV[0]" or die "Can't Tile file: $!";
  13. tie my @check, 'Tie::File', "$ARGV[1]" or die "Can't tie file: $!";
  14.  
  15. # I'm not really sure where newcheckedfile is coming from or what it is.
  16. # Is it a combination of the two files??
  17. $newfile="newcheckedfile";
  18. open (TEXT,">$newfile") || die "cannot open: $!";
  19.  
  20. while(<@check>){
  21. @holder1 = split(/:/);
  22.  
  23.   foreach my $e (@etcshad){
  24.  
  25.   @holder2 = split(/:/, $e);
  26.  
  27.     if( $holder2[0] =~ /^$holder1[0]$/ ){  
  28.      $i++; # This is a shortcut to adding 1 to a number
  29.     }
  30.     if($i){ # 0 is false to perl, anything other than 0 is true.
  31.             # Well except for undef, and "".
  32.     print TEXT $e;
  33.     last;
  34.     }
  35.     }
  36. $i = 0;
  37. }
  38.  
  39.  
  40. untie @etcshad;
  41. untie @check;
  42.  
  43. close(TEXT);
  44. rename("newcheckedfile",$ARGV[1]);
  45.  

I didn't have time to test this code, but you will definitely benefit from reading up on Tie::File.

Happy coding!!
Feb 14 '08 #8
Just for the matter of simplicity, you can use the splice function like this:

Expand|Select|Wrap|Line Numbers
  1.   splice(@tempinfo,0,2,@etcinfo[0,1]); 
  2.  
  3.  

thanks. i got exactly which i need. thanks boss, here the output comes in
single line it self.i need a break in it to print it in next line.
Thanks for ur help......
Feb 14 '08 #9

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

Similar topics

1
by: sam | last post by:
I have a function writen in Perl that takes a name-value list (array) as argument and returns a name-value list (array). My question is: How to call this function from PHP and get the returned...
3
by: dpackwood | last post by:
Hello, I have two different scripts that do pretty much the same thing. The main perl script is on Windows. It runs and in the middle of it, it then calls out another perl script that then...
1
by: Julia Bell | last post by:
I would like to run the same script on two different platforms. The directory in which the script(s) will be stored is common to the two platforms. (I see the same directory contents regardless...
9
by: Martin Foster | last post by:
Hi. I would like to be able to mimic the unix tool 'uniq' within a Perl script. I have a file with entries that look like this 4 10 21 37 58 83 111 145 184 226...
3
by: PzYon | last post by:
Hey 2gether! I'm trying to execute a PERL script on the web server when the user presses a button in an ASP.NET Web Application using Visual Basic. The most obvious solution for me seemed to be...
9
by: 8anos | last post by:
Hello, I am new at the community and newbie at programming :) As you may know rapidshare provides a perl script for linux, to upload files at their servers. You can find the original scripts at...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
10
by: happyse27 | last post by:
Hi All, I got this apache errors(see section A1 and A2 below) when I used a html(see section b below) to activate acctman.pl(see section c below). Section D below is part of the configuration...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
3
by: happyse27 | last post by:
Hi All, I am creating the perl script using html form(with embedded javascript inside). When using this html form with javascript alone, it works where the form validation will pop up...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
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
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.