Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

A small queestion to match processes

by heatblazer (Scribe)
on Oct 05, 2012 at 18:23 UTC ( #997529=perlquestion: print w/ replies, xml ) Need Help??
heatblazer has asked for the wisdom of the Perl Monks concerning the following question:

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.

Update

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.

P.S.

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!

Comment on A small queestion to match processes
Select or Download Code
Re: A small queestion to match processes
by jwkrahn (Monsignor) on Oct 05, 2012 at 18:41 UTC
    #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

    A simpler way to do that:

    $strindex = $-[0] if ?STAT?;

      Thank you, will look into it ASAP. Still unknown syntax to me.

        G'day heatblazer,

        It would have been useful if you specified which syntax you didn't know about. Here's a couple of guesses:

        Also, take a look at perl - perldoc.perl.org. Here you'll find links to all the Perl manual pages. I wouldn't suggest attempting to learn everything in all that documentation; rather, just get a feel for where that information can be found. Once you are familiar with what's available, looking up specifics is usually a fairly easy task.

        -- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://997529]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2015-07-07 01:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (86 votes), past polls