- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- perl command to run on ps -efx
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2006 04:35 AM
11-14-2006 04:35 AM
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..
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2006 04:53 AM
11-14-2006 04:53 AM
Re: perl command to run on ps -efx
For example :
print $files[-1]
regards,
ivan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2006 05:04 AM
11-14-2006 05:04 AM
SolutionIf 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2006 05:06 AM
11-14-2006 05:06 AM
Re: perl command to run on ps -efx
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2006 05:31 AM
11-14-2006 05:31 AM
Re: perl command to run on ps -efx
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2006 05:38 AM
11-15-2006 05:38 AM