awk - grep particular word from output

Hi Experts,

  • Getting error while using it through a variable to get the PID,
PID=42
# UNIX95=1 ps -e -o pcpu,pid,ppid,stime,etime,args | awk '{if ($2~"^42$") print $0}'
 0.00    42     0  Feb 10  600-17:21:29 nfs_async_io
  • But when using with a variable it is not working .
# UNIX95=1 ps -e -o pcpu,pid,ppid,stime,etime,args | awk -v pid1=$PID '{if ($2~^pid1$) print $0}'
 syntax error The source line is 1.
 The error context is
                {if >>>  ($2~^ <<< pid1$) print $0}
 awk: The statement cannot be correctly parsed.
 The source line is 1.

please advise what is wrong happening..

Try ($2==pid1)

EDIT: Or, maybe, ($2 ~ "^"pid1"$") ?

1 Like

Try

"^"pid1"$"

I suppose there is a good reason to do this but, why not just use the the switich to specify the specific pid...so something like

ps -T ${PID} 

and the -o options you are interested in.

Hi blackrageous,

It did not work ,

PID=42
$ ps -T ${PID}
ps: illegal option -- T
usage: ps [-edaxzflP] [-u ulist] [-g glist] [-p plist] [-t tlist] [-R prmgroup] [-Z psetidlist]
$ 

This is ksh on hp-ux 11.23. Please advise,
Thanks,

It's -p for proc list (which I assume was the aim).

1 Like

Try:

UNIX95=1 ps -p "$PID" -o pcpu,pid,ppid,stime,etime,args

-edit- OK, I see CarloM already answered this one..

1 Like

RudiC thanks, worked the first one.
CarolM & Scrutinizer, thanks worked too... ,