http://qs321.pair.com?node_id=1028115


in reply to Re: Module to parse output from Unix ps command?
in thread Module to parse output from Unix ps command?

Using split's third possible argument too instead of just /\s+/, you'd preserve the original spacing of the user command as well (assuming it's always the last column).


Krambambuli
---
  • Comment on Re^2: Module to parse output from Unix ps command?

Replies are listed 'Best First'.
Re^3: Module to parse output from Unix ps command?
by hdb (Monsignor) on Apr 11, 2013 at 11:32 UTC

    Thanks for this comment, I never heard about this third argument. Here is the updated script:

    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 %psdata; while( @ps ) { my $row = shift @ps; chomp $row; $row =~ s/^\s*//; my @row = split /\s+/, $row, $#header+2; my $this_id = splice @row, $posid, 1; @{$psdata{$this_id}}{@header} = @row; } print Dumper( %psdata );