in reply to Re^8: How to fetch the value of unix command result
in thread How to fetch the value of unix command result

Hi Expert,

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| ABC|123|CHECK|1| DEF|456|CHECK|1| GHI|789|CHECK|1| ABC|123|CHECK|1| DEF|456|KCEHC|1| GHI|789|KCEHC|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

#!/usr/bin/env perl 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>; foreach $key(%count){ $c = $count{$key}; $c1 = $count{$key}; }

gives me this output:

$c = 4 $c1 = 4

The output of dumper

$VAR1 = 'CHECK'; $VAR2 = 7; $VAR3 = 'KCHEC'; $VAR4 = 4;

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

i want store the value as CHECK= 7 and KCHEC = 4, separately

Experts any help on this, thanks,regards

Replies are listed 'Best First'.
Re^10: How to fetch the value of unix command result
by poj (Abbot) on Jun 30, 2013 at 15:14 UTC
    Add another line at the end
    print "c=$c c1=$c1\n";
    and see what you get.
    poj
      Hi Experts,

      I have changed the field number now, to fetch the fields 3 and 4 instead of only field 3, then combine the field 3 and field 4 and get the count

      when i execute below the command in unix prompt

      gunzip -c $file|cut -f3,4 -d'|' |sort|uniq -c

      i got the below output

      2 11 CHECK|KCEHC

      My input:

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

      I have tired as below, please check the below code

      $fh = IO::Zlib->new( "$file", 'rb' ) or die "Zlib failed: $!"; $count{$_}++ for map { ( split /\|/ )[3,4] } <$fh>; print Dumper(%count);

      when i executed the script, i got the output as

      $VAR1 = ''; $VAR2 = 1; $VAR3 = 'CHECK'; $VAR4 = 11; $VAR5 = 'KCEHC'; $VAR6 = 11; $VAR7 = '';

      I am expecting my output as below

      CHECK|KCEHC 11

      I want to combine the two keys CHECK and KCEHC and assign a value 11. becuase two keys has same value

        Use join to combine

        $count{$_}++ for map { join '|',( split /\|/ )[2,3] } <$fh>;
        poj
Re^10: How to fetch the value of unix command result
by rajsai28 (Novice) on Jul 02, 2013 at 06:05 UTC
    Hi Experts,

    I dont want to hard code the values like below, because the values may change CHECK, KCHEC to some other values like CHECK1 and KCHEC1, then this will not work, so how can we assign the values without hard coding

    The output of dumper

    $VAR1 = 'CHECK'; $VAR2 = 7; $VAR3 = 'KCHEC'; $VAR4 = 4;
    $c = $count{'KCEHC'}; #4 $c1 = $count{'CHECK'}; #7
    foreach $key(%count){ $c = $count{$key}; $c1 = $count{$key}; }

    The above code, give the output as

    $c = 4; $c1= 4;

    but i want out put as CHECK = 4 and KCHEC =7, how can i get this

    KCHEC=4 CHECK=7

    How can i get this, any suggestion on this

      Hi Experts,

      I dont want to hard code the values like below, because the values may change CHECK, KCHEC to some other values like CHECK1 and KCHEC1, then this will not work, so how can we assign the values without hard coding

      The output of dumper

      $VAR1 = 'CHECK'; $VAR2 = 7; $VAR3 = 'KCHEC'; $VAR4 = 4;
      $c = $count{'KCEHC'}; #4 $c1 = $count{'CHECK'}; #7
      foreach $key(%count){ $c = $count{$key}; $c1 = $count{$key}; }

      The above code, give the output as

      $c = 4; $c1= 4;

      but i want out put as CHECK = 4 and KCHEC =7, how can i get this

      KCHEC=4 CHECK=7

      How can i get this, any suggestion on this