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

Hi Experts,

I am executing unix command 'gunzip -c test.txt.gz |cut -f3 -d'|'|sort|uniq -c' and stroing the value in a variable, the zip file contains records as shown below:

ABC|123|CHECK|1| DEF|456|CHECK|1| GHI|789|CHECK|1| JKL|101|KCEHC|2|

I have used the code as shown below, i am executing the unix commands and storing the value in a variable and from the results i am fetching only numeric value

my $r = `gunzip -c test.txt.gz|cut -f3 -d'|'|sort|uniq -c`; my $c = substr( $r, 6, -1 );

gives me this output:

3 CHECK 1 KCEHC

I am trying to fetch the number 3 from 3 CHECK and 1 from 1 KCEHC,i want store the numbers in a variable separately

I also to grep the CHECK - $r = grep /CHECK/, $r, it returns a 1

Experts any help on this, thanks and regards

Replies are listed 'Best First'.
Re: How to fetch the value of unix command result
by rjt (Curate) on Jun 30, 2013 at 11:00 UTC
    I am trying to fetch the number 3 from 3 CHECK and 1 from 1 KCEHC,i want store the numbers in a variable separately

    Do you mean you want to keep track of how many occurrences of 'CHECK' there are, versus how many of 'KCEHC' there are? A hash would be particularly well suited to this. I will give an example of that in a second, but I'd also like to recommend the following pure-Perl way to accomplish the same thing in your case, as grabbing the same data out of your unix command pipeline output would actually be just as difficult as just generating it in Perl, except it would be longer, less portable, and (probably) less efficient:

    use warnings; use strict; use IO::Zlib; my $fh = IO::Zlib->new('test.txt.gz', 'rb') or die "Zlib failed: $!"; my %count; $count{$_}++ for map { (split /\|/)[2] } <$fh>; printf "%-10s %4d\n", $_, $count{$_} for sort keys %count;

    Output:

    CHECK 3 KCEHC 1

    The following line is the one that answers your question most directly:

        $count{$_}++ for map { (split /\|/)[2] } <$fh>;

    Basically, this can be translated in English as, read a line from $fh, split it on | characters, grab the 3rd field from that (0-based numbering) and then increment (++) the corresponding hash element in %count. ($_ is an automatic variable used in many Perl loop constructs).

      Hi Experts,

      I got the below output when i run the unix command on zip file, this count of CHECK and RECHECK

      2 4 CHECK 7 RECHECK

      I want to store the value 4 and 6 in a scalar variable as follows

      my $c = 4; my $c1 =6;

      This is my code shown below

      my $r = `gunzip -c test.txt.gz|cut -f3 -d'|'|sort|uniq -c`;

      I want to store the values 4 and 6 separately in two separate variables

        I want to store the value 4 and 6 in a scalar variable as follows

        my $c = 4; my $c1 =6;

        Why would you want to go to all the trouble to declare cryptic, arbitrary variables like $c and $c1 when you've already been shown how to dynamically create hash entries that directly map to the keywords in your input file? More to the point, what part of my suggestion was either unacceptable or unclear?

        This is my code shown below

        my $r = `gunzip -c test.txt.gz|cut -f3 -d'|'|sort|uniq -c`;

        This is also the same code you posted in your original node, which makes me wonder how much of an effort you made to either work with my suggestion or continue on your own.

        Hi Experts,

        Below is my code, unix command executes on zip file and displays the count of field 3, based on the value in field 3, i have 10 records in zip file, the field 3 contains word CHECK and KCEHC, the command which is used counts the number of CHECK and KCEHC

        my $result1 = `gunzip -c $file|cut -f3 -d'|'|sort|uniq -c`;

        The input zip file contains, the 11 records

        ABC|123|CHECK|1| DEF|456|CHECK|1| GHI|789|CHECK|1| JKL|101|CHECK|1| ABC|123|CHECK|1| DEF|456|CHECK|1| GHI|789|CHECK|1| JKL|101|KCEHC|1| ABC|123|KCEHC|1| DEF|456|KCEHC|1| GHI|789|KCEHC|1|

        gives me this error message / faulty output:

        2 4 CHECK 6 KCEHC

        I need to store the values 4 in $c and 6 in $c2, could you please suggest how can we do that

        I also tired the below one....

        my $r = `gunzip -c $file|cut -f3 -d'|'|sort|uniq -c`; my $c1 = substr( $r, 16,-2); my $c2 = substr( $, 9,-9);

        This gives me the output as...

        <code> $c1 = 4 $c1 = 6 <code>

        I have a question to experts, was this correct method