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).


In reply to Re: How to fetch the value of unix command result by rjt
in thread How to fetch the value of unix command result by rajsai28

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.