Go Back   CodingForums.com > :: Server side development > Perl/ CGI

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-19-2009, 01:48 PM   PM User | #1
vinodkaushik
New to the CF scene

 
Join Date: Jul 2009
Location: india
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
vinodkaushik is an unknown quantity at this point
How can I compare file list from a tar archive and directory?

I am still learning Perl. Can anyone please suggest me the Perl code to compare files from .tar.gz and a directory path.

Let's say I have tar.gz backup of following directory path which I have taken few days back.

a/file1
a/file2
a/file3
a/b/file4
a/b/file5
a/c/file5
a/b/d/file and so on..

Now I want to compare files and directories under this path with the tar.gz backup file and delete the files in directory which are not available in tar.gz.

Please suggest Perl code to do that.
vinodkaushik is offline   Reply With Quote
Old 08-19-2009, 10:55 PM   PM User | #2
hackYourBrain
New Coder

 
Join Date: Jul 2009
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
hackYourBrain has a little shameless behaviour in the past
Im dont feel like coding this for you right now.. but I dont get what you dont understand?? the syntax?

use shell escapes to pull the output of the ls command in to a couple variables, one for the tar and one for the dir you want to compare.. run the regex.. rm what you dont want.

deadSpace
hackYourBrain is offline   Reply With Quote
Old 08-20-2009, 12:06 AM   PM User | #3
hackYourBrain
New Coder

 
Join Date: Jul 2009
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
hackYourBrain has a little shameless behaviour in the past
what I ment to say is, post your code so people can look at it dude.

TransitCop
hackYourBrain is offline   Reply With Quote
Old 08-20-2009, 07:17 AM   PM User | #4
vinodkaushik
New to the CF scene

 
Join Date: Jul 2009
Location: india
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
vinodkaushik is an unknown quantity at this point
Hi,

I have written below code to compare the files but it is showing all the files in an array2


Code:
#! /usr/bin/perl -w

use strict;
use warnings;
use File::Find;
use Cwd;

my $svnpath = "/home/backup";
my $tarfile = "20090818.tar.gz";

chdir "$svnpath";


my @files;
find(\&d, ".");


sub d {
        -f and -r and push @files, $File::Find::name;
}

sub getTar {
        use Archive::Tar qw();
        my @lists = Archive::Tar->list_archive($tarfile);
        return @lists;
}

my @array1 = &getTar;
my @array2 = @files;

for my $i (@array2) {
           print "$i\n" if grep {$i ne $_} @array1;
}
vinodkaushik is offline   Reply With Quote
Old 08-21-2009, 07:17 PM   PM User | #5
Jolle
New Coder

 
Join Date: Aug 2009
Location: in front of the keyboard
Posts: 17
Thanks: 1
Thanked 1 Time in 1 Post
Jolle is an unknown quantity at this point
Quote:
Originally Posted by vinodkaushik View Post
Hi,

I have written below code to compare the files but it is showing all the files in an array2


Code:
for my $i (@array2) {
           print "$i\n" if grep {$i ne $_} @array1;
}
Off course. grep {$i ne $_} @array1 always returns a list of files that are not equal to $i, so is evaluated true and thus $i prints.

What you want to do is this :
Code:
my @missing = grep { my $x = $_; not grep { $x eq $_ } @array1 } @array2;
@missing contains everything from @array2 that is not in @array1.

Or if you want to do it with a loop :
Code:
for my $i (@array2) {
           print "$i\n" if not grep {$i eq $_} @array1;
}
EDIT :
If you want to see what grep actually returns in your script, execute :
Code:
#! /usr/bin/perl -w

my @array1 = ("file1","file2","file3","file6");
my @array2 = ("file4","file3","file1","file5");

for my $i (@array2) {
           print grep {$i ne $_} @array1;
           print "\n";
}
You realize immediately why it doesn't work.

Last edited by Jolle; 08-21-2009 at 07:26 PM.. Reason: added test program
Jolle is offline   Reply With Quote
Old 08-22-2009, 01:47 AM   PM User | #6
FishMonger
Super Moderator


 
Join Date: May 2005
Location: Southern tip of Silicon Valley
Posts: 2,757
Thanks: 2
Thanked 149 Times in 144 Posts
FishMonger will become famous soon enoughFishMonger will become famous soon enough
Using a hash would be more efficient than grepping over the entire @array1 for each for each item in @array2.

I only partially tested this, so it will probably need a slight tweaking.

Code:
#!/usr/bin/perl

use strict;
use warnings;
use Archive::Tar;
use File::Find;

my $svnpath = "/home/backup";
my $tarfile = "20090818.tar.gz";

my %list = map { $_, 1 } Archive::Tar->list_archive($tarfile);

find(\&d, $svnpath);


sub d {
    if (-f and -r) {
        (my $file = $File::Find::name) =~ s/^$svnpath//;
        print "$file\n" if not exists $list{$file};
    }
}
FishMonger is offline   Reply With Quote
Reply

Bookmarks

Tags
perl

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:26 AM.


Advertisement
Log in to turn off these ads.