Question

I have placed the text file "FilenameKeyword.txt" file in E:/Test folder, in my perl script i am trying to traverse through the folder and am i am trying to find a file with filename which has the string "Keyword" in it, later i have printed the content of that file in my script. Now i wish do the same thing for the file which is placed inside tar file which is compressed.

Hypothetical File from where i am trying to extract the details: E:\test.tar.gz

Wanted to know if there are possibility in perl to search and read the file without decompressing /unzipping the hypothetical file.If that is not possible, I shall also allocate some temperory memory to decompress the file , which should deleted after extracting the content from the particular text file.

While Searching in the internet i could it is possible to extract and read the gzip/tar file by using Archive::Extract, being new to Perl - i am really confused on how actually i should make use of it. Could you please help on this....

Input file:FilenameKeyword.txt

Script:

use warnings;
use strict;

my @dirs = ("E:\\Test\\");
my %seen;
while (my $pwd = shift @dirs) {
        opendir(DIR,"$pwd") or die "Cannot open $pwd\n";
        my @files = readdir(DIR);
        closedir(DIR);
        foreach my $file (@files) 
        {
                if (-d $file and ($file !~ /^\.\.?$/) and !$seen{$file}) 
                {
                        $seen{$file} = 1;
                        push @dirs, "$pwd/$file";
                }
                next if ($file !~ /Keyword/i);
                my $mtime = (stat("$pwd/$file"))[9];
                print "$pwd$file";
                print "\n";
                open (MYFILE, "$pwd$file");
                while (my $line = <MYFILE>){
                #print $line;
                my ($date) = split(/,/,$line,2);
                if ($line =~ s!<messageText>(.+?)</messageText>!!is){
                print "$1";
                }
                }

        }
}

Output(In test program file is placed under E:\Test):

E:\Test\FilenameKeyword.txt
1311 messages Picked from the Queue.

Looking for help to retrieve the content of the file which is place under E:\test.tar.gz

Desired Output:

E:\test.tar.gz\FilenameKeyword.txt
1311 messages Picked from the Queue. 
Was it helpful?

Solution

I was stuck in using CPAN module, CPAN module didn't work for me as i have oracle 10g enterprise edition in the same machine, due do some software conflict Active state perl was unable compile and refer to the perl lib for CPAN module, i have uninstalled oracle in my machine to make this work....

#!/usr/local/bin/perl
use Archive::Tar;
my $tar = Archive::Tar->new;
$tar->read("test.tar.gz");
$tar->extract();

OTHER TIPS

If your file was gzipped only, you could read its contents in a "streamed" manner as outlined here (Piping to/from a child process without system or backtick - gzipped tar files). The article illustrates a technique to use open and a fork to open and decompress the file, and then making it available to Perl's while(), allowing you to iterate over it.

As tar is basically concatenating things, it might be possible to adapt this to your scenario.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top