vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

I have a backup files as log1.tgz, log2.tgz. These files contains only log files, Now I want to search for the particular string to get the log message, without unpack and untar all the file I need to do it, Previously I was unpacking and untar these file and searching to get the information.So that instead of unpack and untar all the file, I can unpack and untar a specific file only

Vinoth,G

Replies are listed 'Best First'.
Re: Read the .tgz file without unpack and untar
by irah (Pilgrim) on May 22, 2009 at 08:59 UTC

    See Archive::Tar module.

    There are some function used to read the content of the file without untar.

Re: Search in the .tgz file without unpack and untar
by Arunbear (Prior) on May 22, 2009 at 09:58 UTC
    Use zcat, e.g.
    % zcat test.tgz | ack foo system("touch foo") system("rm foo") system("rm food") chdir("foo") %
Re: Search in the .tgz file without unpack and untar
by targetsmart (Curate) on May 22, 2009 at 09:23 UTC
    *nix grep utility can help you even in this case.
    UPDATE
    see grep -b
    zcat can see .gz files (compressed with gzip tool)

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
Re: Search in the .tgz file without unpack and untar
by DrHyde (Prior) on May 22, 2009 at 10:12 UTC

    You will still need to uncompress the whole file, at least up to the particular file you're interested in, because That's How gzip Works.

    The best solution would be for you to show us what code you've written so far, and explain the problems you're having - what it does, what you expected it to do, and what else you've tried. Only then can we help you.

Re: Search in the .tgz file without unpack and untar
by shmem (Chancellor) on May 22, 2009 at 09:59 UTC

    You want ack.

Re: Search in the .tgz file without unpack and untar
by Bloodnok (Vicar) on May 22, 2009 at 12:06 UTC
    Try:
    zcat foo.tgz | tar -xvf - some/wanted/file/path
    IIRC, you could try the above with wildcards as well, e.g.
    zcat foo.tgz | tar -xvf - some/wanted/file/path/date*
    The above should facilitate the extraction of all files having date as a prefix

    A user level that continues to overstate my experience :-))
Re: Search in the .tgz file without unpack and untar
by lakshmananindia (Chaplain) on May 22, 2009 at 09:16 UTC

    Not a perl solution

    If you are in unix you can use eval "$(lesspipe)" and then give less yourfilename. It will show you the contents inside the tar.

    --Lakshmanan G.

    The great pleasure in my life is doing what people say you cannot do.


      This is not something you can do "in unix". It is something you might be able to do if you happen to have a lesspipe program installed, but it is in no way a standard behaviour. Most UNIX systems have no such program.
Re: Search in the .tgz file without unpack and untar
by graff (Chancellor) on May 23, 2009 at 00:34 UTC
    If I have a bunch of tar.gz files, and I know these all contain just log file data, and I just want to find and list particular lines that match a given regex pattern -- that is, I don't care which log file(s) are involved, I just want the matching lines -- I would either use a shell command like this:
    gunzip -c *.tgz | grep target_regex > target.hits
    (You might need an extra option, "--binary-files=text" on the grep command, depending on which version of grep you have.)

    Or else I'd write a perl script like this:

    #!/usr/bin/perl use strict; use PerlIO:gzip; for my $file (<*.tgz>) { open( T, "<:gzip", $file ) or do { warn "open failed on $file: $!\n"; next; }; while (<T>) { print if /target_regex/; } }
    (And in the latter case, I'd probably use @ARGV to set the path for where to find the tgz files, what regex to apply, and/or whether to read the list of tgz file names from STDIN, so that the script serves a general range of uses.)