Re: Compare two files
2007/10/5, [EMAIL PROTECTED] [EMAIL PROTECTED]: Hi I would like to compare two files by comparing the files dates. If one file shows ls -la May 12 2003 filename and the other name shows the same date they are OK for me (I'm not interested in the time part only the date of the file) But if the dates are not the same I would like to copy one of the files. How do I do this in Perl? Hi, if ( int(-M file1.txt) != int(-M file2.txt) ) { # copy the file } for copying files,you can use the module File::Copy from CPAN, http://search.cpan.org/~nwclark/perl-5.8.8/lib/File/Copy.pm -- Jeff Pang - [EMAIL PROTECTED] http://www.rwweb.co.cc -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Compare two files
2007/10/5, Jeff Pang [EMAIL PROTECTED]: 2007/10/5, [EMAIL PROTECTED] [EMAIL PROTECTED]: How do I do this in Perl? Hi, if ( int(-M file1.txt) != int(-M file2.txt) ) { # copy the file } Forgot to tell you what's -M in perl,from `perldoc perlfunc`: -M Script start time minus file modification time, in days. -- Jeff Pang - [EMAIL PROTECTED] http://www.rwweb.co.cc -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Compare two files
Jeff Pang wrote: 2007/10/5, [EMAIL PROTECTED] [EMAIL PROTECTED]: I would like to compare two files by comparing the files dates. If one file shows ls -la May 12 2003 filename and the other name shows the same date they are OK for me (I'm not interested in the time part only the date of the file) But if the dates are not the same I would like to copy one of the files. How do I do this in Perl? if ( int(-M file1.txt) != int(-M file2.txt) ) { # copy the file } That might be true also when the dates are the same. I would use stat(), localtime() and timelocal() instead. use Time::Local; my $date1 = timelocal 0,0,0,(localtime( (stat 'file1.txt')[9] ))[3..5]; my $date2 = timelocal 0,0,0,(localtime( (stat 'file2.txt')[9] ))[3..5]; if ( $date1 != $date2 ) { # copy the file } -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Compare two files
2007/10/5, Paul Lalli [EMAIL PROTECTED]: On Oct 5, 7:50 am, [EMAIL PROTECTED] (Jeff Pang) wrote: 2007/10/5, [EMAIL PROTECTED] [EMAIL PROTECTED]: I would like to compare two files by comparing the files dates. If one file shows ls -la May 12 2003 filename and the other name shows the same date they are OK for me (I'm not interested in the time part only the date of the file) But if the dates are not the same I would like to copy one of the files. How do I do this in Perl? if ( int(-M file1.txt) != int(-M file2.txt) ) { # copy the file } That's a very naïve approach that will frequently fail. For example: File one modified 10/1/2007 9am File two modified 10/1/2007 3pm and the current time is 10/2/2007 12pm -M 'file1' will return 1.125 -M 'file2' will return 0.875. int(1.25) == 1 int(0.875) == 0 You're right here. sub m_to_date { my $days_ago = shift; my $ts = time() - ($days_ago * 24 * 60 * 60); my $date = strftime('%Y-%m-%d', localtime($ts)); return $date; } -M is not needed here.to get the date,one can just stat it, sub get_date { my $file = shift; my $mtime = (stat $file)[9]; return strftime '%Y-%m-%d', localtime($mtime); } -- Jeff Pang - [EMAIL PROTECTED] http://www.rwweb.co.cc -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Compare two files
On Oct 5, 7:50 am, [EMAIL PROTECTED] (Jeff Pang) wrote: 2007/10/5, [EMAIL PROTECTED] [EMAIL PROTECTED]: I would like to compare two files by comparing the files dates. If one file shows ls -la May 12 2003 filename and the other name shows the same date they are OK for me (I'm not interested in the time part only the date of the file) But if the dates are not the same I would like to copy one of the files. How do I do this in Perl? if ( int(-M file1.txt) != int(-M file2.txt) ) { # copy the file } That's a very naïve approach that will frequently fail. For example: File one modified 10/1/2007 9am File two modified 10/1/2007 3pm and the current time is 10/2/2007 12pm -M 'file1' will return 1.125 -M 'file2' will return 0.875. int(1.25) == 1 int(0.875) == 0 There are two possible correct solutions. One is to actually execute `ls -l` for each file and parse the output. The other is to take the return values of the -M calls, subtract them from the current unix time stamp, convert both times to date strings, and then compare the dates. For example: #!/usr/bin/perl use strict; use warnings; use POSIX qw/strftime/; my $date1 = m_to_date(-M 'file1.txt'); my $date2 = m_to_date(-M 'file2.txt'); if ($date1 eq $date2) { # copy the file } sub m_to_date { my $days_ago = shift; my $ts = time() - ($days_ago * 24 * 60 * 60); my $date = strftime('%Y-%m-%d', localtime($ts)); return $date; } __END__ Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Compare two files
[EMAIL PROTECTED] wrote: Hi Hello, I would like to compare two files by comparing the files dates. If one file shows ls -la May 12 2003 filename and the other name shows the same date they are OK for me (I'm not interested in the time part only the date of the file) But if the dates are not the same I would like to copy one of the files. How do I do this in Perl? if (file1 not equal in date file2) { cp file 1 someplace } ( my $date1 = localtime +( stat 'file1' )[ 9 ] ) =~ s/\d+:\d+:\d+//; ( my $date2 = localtime +( stat 'file2' )[ 9 ] ) =~ s/\d+:\d+:\d+//; if ( $date1 ne $date2 ) { # copy 'file1', ... } John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order.-- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: compare two files
On Oct 11, 2005, at 11:18, Jeff Pan wrote: Is there a best way to find some lines which exists in both two files? Someone has show me a method but it seems a little low efficiency. The code is as below: Have you searched CPAN for diff modules? -- fxn -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: compare two files
Jeff Pan wrote: hi, Hello, Is there a best way to find some lines which exists in both two files? Your code logic says that you want to find lines in '2.txt' that are NOT in '1.txt'. Someone has show me a method but it seems a little low efficiency. Not only that but it doesn't work correctly. If a line is not found in '1.txt' then diff() will ALWAYS return 0. The code is as below: open (T1,1.txt) or die $!; open (T2,2.txt) or die $!; while(my $line=T2){ chomp $line; print $line,\n unless diff($line); } sub diff { my $line=shift; my $ok=0; while(T1){ chomp; if ($_ eq $line) { $ok=1; last; } } return $ok; } close T2; close T1; The usual way to do this is to use a hash: open T1, '', '1.txt' or die open '1.txt' $!; open T2, '', '2.txt' or die open '2.txt' $!; my %hash; while ( T2 ) { $hash{ $_ }++ } while ( T1 ) { print if exists $hash{ $_ } } __END__ John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: compare two files
Jeff Pan wrote: hi, Is there a best way to find some lines which exists in both two files? I usually use the Unix comm(1) utility. Its very efficient, but the input files need to be sorted. Google for ppt comm and you can find a Perl version. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: compare two files
Your code logic says that you want to find lines in '2.txt' that are NOT in '1.txt'. thanks for all answerers. yeah,I have put less attention on that code,and have made a fatal logic error. but the way mentioned by John, putting file's all contents to a hash, maybe somewhat less efficiency when the file is large much.is it? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response