Combining awk command to make it more efficient

VARIABLE="jhovan    5259  5241  0 20:11 ?        00:00:00 /proc/self/exe --type=gpu-process --channel=5182.0.1597089149 --supports-dual-gpus=false --gpu-driver-bug-workarounds=2,45,57 --disable-accelerated-video-decode --gpu-vendor-id=0x80ee --gpu-device-id=0xbeef --gpu-driver-vendor --gpu-driver-version --v8-natives-passed-by-fd --v8-snapshot-passed-by-fd"

current command i'm using:

echo "${VARIABLE}" | awk '/channel=5182/ && !/egrep|ps -ef|grep / && ($0 ~ /1597/) {print $0}' | awk -F"1597089149" '{print $2}' | awk '{print $1}'

How can i optimize this code? i'd like to do everything with one awk command? possibly even avoiding the "echo".

the expected output should be:

--supports-dual-gpus=false

try this

ps -ef | awk '/[c]hannel=5182/{for(i=1;i<=NF;i++)if($i~/supports/)print $i}'
1 Like

this looks like it could work. but it assumes "supports" will always be in the position it is grabbed from. what im trying to do is print any pattern that is present in the position that "supports" is in.

for instance. the following text may be in the position:

/bin/ksh fashsearch.sh giraffes are great animals

in which case, what i want to do is print fastsearch.sh. essentially do what the following code is doing, but instead of using multiple commands, i'd like just one instance of awk to do everything.

ps -ef | awk -F"/bin/ksh" '{print $2}' | awk '{print $1}'

Perhaps something more like:

pattern="/bin/ksh"
ps -ef | awk -v pat="$pattern" '{for(i=1;i<NF;i++)if($i~pat)print $(i+1)}'

pattern="channel=5182"
ps -ef | awk -v pat="$pattern" '{for(i=1;i<NF;i++)if($i~pat)print $(i+1)}'

will give you what you want???

1 Like