in reply to How to fetch the value of unix command result
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).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to fetch the value of unix command result
by rajsai28 (Novice) on Jun 30, 2013 at 11:46 UTC | |
by rjt (Curate) on Jun 30, 2013 at 12:22 UTC | |
by rajsai28 (Novice) on Jun 30, 2013 at 12:01 UTC | |
by rjt (Curate) on Jun 30, 2013 at 12:45 UTC | |
by rajsai28 (Novice) on Jun 30, 2013 at 13:07 UTC | |
by poj (Abbot) on Jun 30, 2013 at 13:42 UTC | |
|