Operating System - HP-UX
1833758 Members
3029 Online
110063 Solutions
New Discussion

perl command to run on ps -efx

 
SOLVED
Go to solution
Ratzie
Super Advisor

perl command to run on ps -efx

I need to run a command from the last column on a ps -efx command.

So far I can get to print out:
#!/usr/bin/perl
use warnings;
use strict;

open (PIPE, "ps -efx|grep answerd|grep -v grep|") or die "Pipe open failed\n";
my @files =;
close (PIPE);

print @files;

And this gives me:
admin 292 1 0 10:23:09 ? 0:00 /usr/bin/answerd acdb ANSA_SLKR_5_ALL_ALL_121
admin 240 1 0 10:23:09 ? 0:00 /usr/bin/answerd acdb ANSA_BRND_TO_5_ALL_ALL_103
admin 345 1 0 10:23:11 ? 0:00 /usr/bin/answerd acdb ANSA_THSN_TO_5_ALL_ALL_133
admin 322 1 0 10:23:10 ? 0:00 /usr/bin/answerd acdb ANSA_THSN_0_ALL_ALL_130

What I want to do is take the last column, say:
ANSA_THSN_0_ALL_ALL_130

and run a command with this variable...
answerd acdb -s ANSA_THSN_0_ALL_ALL_130

I kinda figured I need to split, but how do I do an array?

I though of using ARGV as well..
5 REPLIES 5
Ivan Krastev
Honored Contributor

Re: perl command to run on ps -efx

If your list is in array its simple to get last element from array with index -1.
For example :

print $files[-1]



regards,
ivan
Hein van den Heuvel
Honored Contributor
Solution

Re: perl command to run on ps -efx

Hello Laura,

If it *always* the last column, then you can use an array reference like Ivan almost explained. Untested:

while () {
@words = split;
$last = @words - 1;
print "test $words[$last]\n";
}


I prefect to test for a more explicit pattern.
And you might as well roll the 'grep' stuff into perl. Perl is good at grepping!

Try (again untested):

foreach (`ps -efx`) {
if (/:\d+\s\/usr\/bin\/answerd.*\s(\w+)$/) {
print "test $1\n";
}

The regexp looks for a line with
- :\d+\s ... that's the tail of time 0:00 before image
- \/usr\/bin\/answerd ... the image with escaped directory slashes.
- .* ... anything
- \s(\w+)$ ... a space followed by a word at the end of the line.
The parens make perl remember that word in $1

hth,
Hein.
James R. Ferguson
Acclaimed Contributor

Re: perl command to run on ps -efx

Hi:

When you 'split' you can reference the number of elements (zero relative) returned. Hence:

# echo "a b c d e f"| perl -nle '@a=split;print $a[$#a]'

...returns "f".

If you wish to use the autosplit mode:

# echo "a b c d e f"| perl -nale 'print $F[$#F]'

...returns "f", too.

Regards!

...JRF...

Hein van den Heuvel
Honored Contributor

Re: perl command to run on ps -efx

Do you need the ps lines in an array, or just the last words?

You can alwasy 'push' those onto the array once properly selected and cleaned:
push @arguments, $last_word;

>> I prefect to test for a more explicit pattern.

Bad typing.. I prefer

>> $last = @words - 1;

@words is using the array words in scalar context returning the element count, which is one higher than the highest element number (0 based).

As Ivan shows, just using -1 works well for last.

You probably want to issue a 'chomp' as the first line in hte loop, to get of the newline which will otherwise go with the last word.

Hein.
Ratzie
Super Advisor

Re: perl command to run on ps -efx

perfect