Parsing results of ps?

M

Marv

Hello,

I'm a perl beginner and having trouble with what would appear like a
simple task.

I need to capture PID number from each line into a variable while in a
loop. The help I need is how to get to split each line of the output
so that I get the PID number regardless of how many digits long it is.
The output of ps -x gives me the pid number in the first column,
however it seems to be right justified in this column as such:

2193 ? S blahblah blah
26127 pts/0 S blahblah blah
4513 ? S blahblah blah

So when I try to do a split using a space as the delimiter some of the
variables end up empty.

How could I do this so that I always end up with the PID number?

Thanks,
Marv
 
K

ko_

Originally posted by Marv
Hello,

I'm a perl beginner and having trouble with what would appear like a
simple task.

I need to capture PID number from each line into a variable while in a
loop. The help I need is how to get to split each line of the output
so that I get the PID number regardless of how many digits long it is.
The output of ps -x gives me the pid number in the first column,
however it seems to be right justified in this column as such:

2193 ? S blahblah blah
26127 pts/0 S blahblah blah
4513 ? S blahblah blah

So when I try to do a split using a space as the delimiter some of the
variables end up empty.

How could I do this so that I always end up with the PID number?


Marv



If you split $_ without using a pattern leading whitespace is ignored,
so you can do something like this:



#!/usr/bin/perl -w

use strict;



my @arr = <DATA>;



foreach (@arr) {

my($pid) = split;

print "$pid\n" if defined $pid;

}



__DATA__

2193 ? S blahblah blah

26127 pts/0 S blahblah blah

4513 ? S blahblah blah



This is explained in 'perldoc -f split'



HTH - keith
 
G

Gregory Toomey

It was a dark and stormy night, and Marv managed to scribble:
Hello,

I'm a perl beginner and having trouble with what would appear like a
simple task.

I need to capture PID number from each line into a variable while in a
loop. The help I need is how to get to split each line of the output
so that I get the PID number regardless of how many digits long it is.
The output of ps -x gives me the pid number in the first column,
however it seems to be right justified in this column as such:

2193 ? S blahblah blah
26127 pts/0 S blahblah blah
4513 ? S blahblah blah

So when I try to do a split using a space as the delimiter some of the
variables end up empty.

How could I do this so that I always end up with the PID number?

Thanks,
Marv

Here's a snippet of code (based on linux ps) that gives you a start:

my($pid,$tname,$etime,$cmd,$emin,$ehour,$background);
for (split '\n', qx(ps -u $> -o pid,tname,etime,cmd --no-headers)) {
#unpack ps line
($pid, $tname, $etime,$cmd) = unpack "a5 x a11 x a8 x a200",$_;
# decode a few fields
$etime =~ /((\d*):)?(\d*):(\d*)$/;
$ehour = $2;
$emin = $3;
# is this process running in the background
$background=/\?/;
#etc
}


gtoomey
 
J

Jack D.

Marv said:
Hello,

I'm a perl beginner and having trouble with what would appear like a
simple task.

I need to capture PID number from each line into a variable while in a
loop. The help I need is how to get to split each line of the output
so that I get the PID number regardless of how many digits long it is.
The output of ps -x gives me the pid number in the first column,
however it seems to be right justified in this column as such:

2193 ? S blahblah blah
26127 pts/0 S blahblah blah
4513 ? S blahblah blah

So when I try to do a split using a space as the delimiter some of the
variables end up empty.

How could I do this so that I always end up with the PID number?

I have had great success with:

http://search.cpan.org/~durist/Proc-ProcessTable-0.39/

It does the splitting for you!

Jack
 
A

Anno Siegel

Gregory Toomey said:
It was a dark and stormy night, and Marv managed to scribble:
I need to capture PID number from each line into a variable while in a
[...]

Here's a snippet of code (based on linux ps) that gives you a start:

my($pid,$tname,$etime,$cmd,$emin,$ehour,$background);

Some of these variables aren't used in your code. Those that are used
are only used inside the loop body, so the declaration should be in the
body too (principle of smallest possible scope).
for (split '\n', qx(ps -u $> -o pid,tname,etime,cmd --no-headers)) {

There is no need for split, qx() does that automatically in list context:

for ( qx(ps -u $> -o pid,tname,etime,cmd --no-headers) ) {
#unpack ps line
($pid, $tname, $etime,$cmd) = unpack "a5 x a11 x a8 x a200",$_;
# decode a few fields
$etime =~ /((\d*):)?(\d*):(\d*)$/;
$ehour = $2;
$emin = $3;
# is this process running in the background
$background=/\?/;
#etc
}

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top