soubalaji has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Iam working in unix platform. I am using the below command in Unix terminal and it's working fine.

gzgrep "00380-1037" ./paz0*/home/fdm*/logs/*CYUTI*21* | grep "isTempDisabled Y"


But iam unable to use the same in perl, i tried this with Backtick, but it's not working. Kindly suggest me how to use this unix command in perl

Replies are listed 'Best First'.
Re: Use Unix command in Perl
by Anonymous Monk on May 25, 2011 at 06:41 UTC
Re: Use Unix command in Perl
by fidesachates (Monk) on May 25, 2011 at 13:37 UTC
    I have 2 suggestions to help you debug.

    1. Use the full path for the system commands. I.e. gzgrep and grep.
    2. Start small. Cut your command down to just gzgrep "00380-1037" <specific file without *> Then once you verify that command works (if it doesn't, I would suggest you have a fundamental problem which needs addressing), add on to it until you find a spot that doesn't work. Then you know what the problem is more likely to be.
Re: Use Unix command in Perl
by anonymized user 468275 (Curate) on May 25, 2011 at 09:24 UTC
    According to perlop, "a string enclosed by backticks (grave accents) first undergoes double-quote interpolation". This suggests doing something like:
    my $shcom = 'gzgrep "00380-1037" ./paz0*/home/fdm*/logs/*CYUTI*21* | g +rep "isTempDisabled Y"'; my $grepped = `$shcom`;

    One world, one people

      soubalaji does not need double quote interpolation, as there are no perl variables that might need expansion embedded in his command string.

      Instead he needs glob expansion. ./paz0*/home/fdm*/lo­gs/*CYUTI*21* presumably expands to a long list of matching files, which he wants to filter with gzgrep(*). Running a command via backticks ought to invoke a shell to do the expansion, however as I recall the shell invoked is sh which has few features compared with bash or the like.

      If it where me, I would implement the whole thing in perl, that is use glob or File::Glob to expand the glob patten to a list of files, then open each through a zcat filter searching for matching lines.

      Alternatively, soubalaji could use perl to expand the glob, feed that list into gzgrep, and then capture and filter the output. Something like:

      my @file_list = glob './paz0*/home/fdm*/lo­gs/*CYUTI*21*'; my $command = 'gzgrep "00380-1037" ".join(' ',@file_list); open my $cmdFH, '-|', $command or die "Error running gzgrep $!"; my @matches = {grep qr/isTempDisabled Y/} <$cmdFH>; close $cmdFH; print @matches;

      Note that the second use of grep is the internal perl function, and it is used to filter the list of matching lines that we get when the filehandle on the gzgrep command is called in an array context.

      (*) could gzgrep be a typo? On my system the command to grep gzip compressed files is zgrep

        I didn't offer the interpolation as a suggestion and your definition of it is insufficient - otherwise he wouldn't be having the problem in the first place.

        One world, one people