Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: How to awk a grep result in-script

by marioroy (Parson)
on Nov 16, 2019 at 03:29 UTC ( #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
Node Status?
node history
Node Type: note [id://11108773]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2020-09-27 02:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (142 votes). Check out past polls.

    Notices?