Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (14)
    As of 2018-06-22 17:46 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (124 votes). Check out past polls.