Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!mips!apple!oliveb!orc!mipos3!iwarp.intel.com!news From: merlyn@iwarp.intel.com (Randal Schwartz) Newsgroups: comp.lang.perl Subject: 'pt'... simple process tree in Perl Message-ID: <1990Mar7.190901.3862@iwarp.intel.com> Date: 7 Mar 90 19:09:01 GMT Sender: news@iwarp.intel.com Reply-To: merlyn@iwarp.intel.com (Randal Schwartz) Organization: Stonehenge; netaccess via Intel, Beaverton, Oregon, USA Lines: 39 Here's a simple little goodie to sort the output of a process listing so that it shows all parents immediately before their offspring. If someone wants to make it a tree, feel free. ==================================================snip here #!/local/merlyn/bin/perl @x = split(/\n/,`ps lagxc`); $header = shift @x; $offpid = index($header,' PID'); $offppid = index($header,' PPID'); for (@x) { $pid = sprintf("%05d", 0 + substr($_,$offpid,5)); $ppid = sprintf("%05d", 0 + substr($_,$offppid,5)); $text{$pid} = $_; $parent{$pid} = $ppid; $children{$ppid} .= "$pid " if $pid > 0; } print "$header\n"; for $each (sort keys (text)) { next unless ($each == 0) || undef $text{$parent{$each}}; @x = ($each); while ($#x >= $[) { $_ = shift(@x); print "$text{$_}\n"; @x = (sort split(/ /, $children{$_}),@x); } } ================================================== snip here Just another Perl hacker, -- /=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\ | on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III | | merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn | \=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/