#!/usr/bin/perl -w use strict; open(PS, "ps aux|") || die "Can`t open PS filehand: $!\n"; my $strindex; #still a global var... argh! my %prochash=(); while ( ) { #obtain the length to match according to ps aux { print "#Enter the block just once\n"; last if $strindex; $strindex = index($_, "STAT"); #obtain how characters the RegEx should not match } #no more of this besides the first line if ( /^.{$strindex}(?.{1,4})\s){1}?/i ) { $prochash{$+{stats}}++; #count processes status } } close(LS); #or don`t if you want to maintain the PSes while (my($k, $v) = each(%prochash) ) { print "$k is $v times \n"; } #### #!/usr/bin/perl -w ##Copyright yours truly Ilian Z. use strict; sub handler { print "Interupt catch\n"; die "Dead...\n"; } =pod open LS, "ps aux|" or die "Can`t open filehandle: $!\n"; my %phash=(); my $strm; while ( ) { { print "#enter the block"; if ( $strm ) { last; } $strm = index($_, "STAT"); } if ( /^.{$strm}(?.{1,4}\s){1}?/i ) { $phash{$+{status}}++; } } close LS; while ( my($k, $v) = each(%phash) ) { print $k, " ===> ", $v, "\n"; } =cut open TOP, "top|" || die "Can`t open 'top' command: $!\n"; my %pnc = (); my @tmp=(); my ($pid, $command); while ( my $match = ) { { next if $command > 0 ; $command = index($match, "COMMAND"); $command++; # end block after 1st verify } next if $command < 1 ; #if we don`t match the index let`s skip below #Here we make a process ID the key and the command the value #a bit ugly but normal for Camel ($pnc{($match =~ /(?\d+)/) ? $+{pid} : 0} = ($match =~ /^.{$command}\s(?.+)/i)?$+{cmd} : 0 ) || next; } close TOP; while ( my ($k, $v) = each(%pnc) ) { if ( defined $k and defined $v ) { print "Proc.ID#",$k," runs command: ", $v,"\n"; } }