Avoid printing entire line if string not found

so im searching the process table with:

ps -ef | awk -F"./rello.java" '{ print substr($0, index($0,$2)) }'

I only want it to print everything that's infront of the "./rello.java". That's because im basically getting the arguments that was passed to the rello.java script.

this works.

Example:

if the process table contains this:

root     31693 15875  0 01:35 pts/1    00:00:00 /bin/sh ./rello.java ff1 ff2 aha lla auu

The awk code should only return this:

 ff1 ff2 aha lla auu

however, when there's no arguments, or nothing in front of the "rello.java", the awk code prints out the entire line from the process table that matches the rello.java:

root     31693 15875  0 01:35 pts/1    00:00:00 /bin/sh ./rello.java

i only want it to print what's in front of rello.java IF there is actually something there. if not, i dont want it to print out anything.

ps -ef | awk -F"./rello.java" 'NF > 1 && ! /awk/ {print ($2 ~ /^ *$/) ? $1 FS : $2}'
2 Likes

Pipe it through

awk 'match ($0, /rello.java [^ ]*/) {print substr ($0, RSTART+11)}'
1 Like