Filesystem pattern match in awk

Hi, I'm trying to grep appln processes using its filesystem and also using awk to get accurate results, however when i'm uisng the filesystem in awk statement i'm getting error. Requesting help.

ps -eaf | grep ApplnName | awk '/ /opt/xxx/yyy / { print }'

Trying with this above code; getting error

syntax error The source line is 1.
 The error context is
                / /opt/xxx/yyy / >>>  { <<<
 awk: Quitting

also tried ps -eaf | grep ApplnName | awk '/ \/opt/xxx/yyy\/ { print }' but getting same error

Hello sam_bd,

Could you please try following and let me know if this helps you(Not tested though).

 ps -eaf | grep ApplnName | awk '/\/opt\/xxx\/yyy/'
 

Thanks,
R. Singh

1 Like

Or

ps -eaf | grep '[A]pplnName' | awk '$0~"/opt/xxx/yyy"'

Or a pure string search (no ERE)

ps -eaf | grep '[A]pplnName' | awk 'index($0,"/opt/xxx/yyy")'

{print} is the default action in awk.
The [ ] trick prevents from matching the grep argument in the ps list.

1 Like

Gents, why the grep ?

ps -eaf | awk '/[Aa]pplname/ && /\/opt\/xxx\/yyy/'
1 Like

sorry for delay. Ravinder: yes, your suggested code did work; Thank you.
M.i.Germany: ps -eaf | grep '[A]pplnName' | awk 'index($0,"/opt/xxx/yyy")' worked for me. Thank you.
RudiC: your suggestion did work for me. Learnt a new awk thing regarding selecting processes. Thank you.

---------- Post updated at 05:14 PM ---------- Previous update was at 05:00 PM ----------

but one thing i'm not clear is the option -x which shows command line in extended format for the "ps" command. I saw an "-x" added to "ps" command in another script. I just added "-x" to the code suggested above by you all. And i got the exact count (11) of the processes for that ApplnName. If i didn't add "-x", i am getting the correct output but with 3 other processes missed. Not clear what difference "-x" made? I did go thru the 'man' page of 'ps' command and got to know that '-x' shows command line in extended format. But not very clear about it.

In fact, this is NOT an " awk thing regarding selecting processes" - it's one of the strengthes of awk to match regexes found in the input (text) stream. ps output IS text, and it happens to contain the "applname" word (or not).

Depending on the system that you work on - which you fail to mention, BTW - ps comes in various flavours. Guessing from the options you use in post#1, you're on a linux system? From its man ps :

-x is one of them - read carefully what it really does for you. Me personally, I don't think its needed (in non-BSD systems).

I'm on HP-UX B.11.23. Amazed to see the function of awk in this case & realized output of ps command is also a data in tabular format which awk deals with. Need to dig deeper in awk.

The -x option is specific to HP-UX and makes a wider output, similar to the w option in the BSD ps.