Efficient way to combine command

im currently running the following command to grab all arguments in front of a script, directly from the process table.

# cat /tmp/allmyprocs
ubuntu    9933 27793  0 03:29 pts/0    00:00:00 /bin/sh ./prying.sh
ubuntu    9941  9933  0 03:29 pts/0    00:00:00 sh
ubuntu    9952  9941  0 03:29 pts/0    00:00:00 sh
myncurpid=9933
AFILENAME=prying.sh

cat /tmp/allmyprocs | awk -v JAG=9933 '$0 ~/ '${myncurpid}' / && ($2 == JAG) {print $0}' | awk -F"prying.sh" '{print substr($0,index($0,$NF))}'

which produces:

ubuntu    9933 27793  0 03:29 pts/0    00:00:00 /bin/sh ./prying.sh

Whenever there are no arguments passed to the "prying.sh" script, the above code outputs the entire line from the ps -ef command. I dont want that to happen. i dont want the entire line to be outputted whenever arguments are not passed.

In other words:

If there are no arguments passed to the script, print nothing.
If there are arguments passed to the script, print only those arguments.

my command is suppose to grab everything in front of prying.sh to the end of the line. but its not doing that.

If I understand what you're trying to do, and I'm not sure that I do (since you didn't supply any sample input that would cause anything to be output), you might want to try the following:

#!/bin/ksh
file=${1:-/tmp/allmyprocs}
myncurpid=9933
AFILENAME=prying.sh

awk -v JAG="$myncurpid" -v AFILENAME="$AFILENAME" '
(off = index($0, AFILENAME " ")) && $2 == JAG {
	print substr($0, off + length(AFILENAME) + 1)
}' "$file"

If you really want the leading space before the list of arguments passed to prying.sh remove the + 1 from the substr() .

One might guess that you're using an Ubuntu Linux distribution from your command prompt, but you haven't given us any indication of what shell you're using. Although written and tested using a Korn shell, the above should work with any POSIX-conforming shell. If you're using a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk .