Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: How to awk a grep result in-script

by marioroy (Prior)
on Nov 16, 2019 at 03:29 UTC ( [id://11108773]=note: print w/replies, xml ) Need Help??


in reply to How to awk a grep result in-script

Hi, ShipWreck

Here are Perl snippets resembling awk and grep. The open statement emits a warning to the terminal if the command doesn't exist. Thus the reason simply exiting with status 127 matching the status in shell.

Shell

$ ps -ef | awk '{ print $1,$2,$8 }' | grep '^kdfadm ' $ ps -ef | awk '{ if ($1 == "kdfadm") print $1,$2,$8 }'

Perl (no delays, fast like awk)

use strict; use warnings; $, = ' '; # set output field separator $\ = "\n"; # set output record separator open my $cmd, 'ps -ef |' or exit(127); while (<$cmd>) { my ($f1,$f2,undef,undef,undef,undef,undef,$f8) = split(' ',$_,-1); print $f1,$f2,$f8 if ($f1 eq 'kdfadm'); } close $cmd;

Perl using Proc::ProcessTable (suggestion by haukex and 1nickt)

use strict; use warnings; use Proc::ProcessTable; $, = ' '; # set output field separator $\ = "\n"; # set output record separator my $t = Proc::ProcessTable->new(); my $uid = getpwnam('kdfadm'); foreach my $p (@{ $t->table }) { print @{ $p }{qw( uid pid cmndline )} if ($p->uid == $uid); }

Regards, Mario

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11108773]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-26 06:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found