in reply to Module to parse output from Unix ps command?
It was easier said than done...
use strict; use warnings; use Data::Dumper; my $command = "ps -f"; my $id = "PID"; my @ps =`$command`; $ps[0] =~ s/^\s*//; my @header = split /\s+/, shift @ps; my ($posid) = grep { $header[$_] eq $id } 0..$#header; die "$id: No such column!\n" unless defined( $posid ); splice @header, $posid, 1; my $n = (scalar @header) - 1; my %psdata; while( @ps ) { my $row = shift @ps; $row =~ s/^\s*//; my @row = split /\s+/, $row; my $this_id = splice @row, $posid, 1; @{$psdata{$this_id}}{@header[0..$n-1]} = splice @row, 0, $n; $psdata{$this_id}{$header[$n]} = join " ", @row; # inaccurate as it +just pastes everything together } print Dumper( %psdata );
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Module to parse output from Unix ps command?
by Krambambuli (Curate) on Apr 11, 2013 at 10:47 UTC | |
by hdb (Monsignor) on Apr 11, 2013 at 11:32 UTC |
In Section
Seekers of Perl Wisdom