Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Comment on


Hello, monks

I have a fast question:

I want to match unix`s ps aux, and I have some idea I realized and it is working, I am just wondering if the approach is good, decent or poor noob-a-like.

#!/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 ( <PS> ) { #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}(?<stats>.{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"; }

Any better ideas will be most appriciated. But not too advanced stuff since I am still a newbie.


I was able to parse "top" command too assigning it to a dseired by me hash. Here is the codeblock:

#!/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 ( <LS> ) { { print "#enter the block"; if ( $strm ) { last; } $strm = index($_, "STAT"); } if ( /^.{$strm}(?<status>.{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 = <TOP> ) { { 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 =~ /(?<pid>\d+)/) ? $+{pid} : 0} = ($match =~ /^.{$command}\s(?<cmd>.+)/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"; } }

Hope that some UNIX sys admins will benefit from the idea and the code.

You won`t see anything in the terminal, just press 'Q' sometime to see the hash assignment. It`s a baby code and there can be many options to it. If I get better I`ll definitely modularize it and hope to help some world`s problems.


I had no idea that parsing TOP is actually not exactly what you think and what you see in the terminal. After top >> top.log and review it in texteditor I saw many control characters so  /^(\d+)\s/ never worked since no new line began with a digit but a control character!!! Awesomely godness! Never knew that stuff!

In reply to A small queestion to match processes by heatblazer

and:  <code> code here </code>

  • Please read these before you post! —
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (15)
    As of 2014-07-10 21:27 GMT
    Find Nodes?
      Voting Booth?

      When choosing user names for websites, I prefer to use:

      Results (216 votes), past polls