Efficient way to combine command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Efficient way to combine command
# 1  
Old 01-07-2018
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.

Code:
# 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

Code:
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.
# 2  
Old 01-08-2018
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:
Code:
#!/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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. Shell Programming and Scripting

Combine sed command

Hi I have command and i want to short it. tr -c '' '' | sed 's/---/-/g' I want one sed command it is possible to include tr command in to sed Thanks (8 Replies)
Discussion started by: asavaliya
8 Replies

3. UNIX for Advanced & Expert Users

Which cut command is more efficient?

Hi, I've got a query regarding which of the following is more efficient & why - cat <filename>|cut -d'*' -f2- > <newfilename> or cut -d'*' -f2- <filename> > <newfilename> Thanks. (17 Replies)
Discussion started by: sumoka
17 Replies

4. UNIX for Dummies Questions & Answers

What is the command use to combine line using sed

Hi all, What is the sed command use to combine line? Example: Below is an output after extracted from few commands aaa bbb ccc ddd eee fff ggg and i would like to combine all the line as shown below, aaa,bbb,ccc,ddd,eee,fff (5 Replies)
Discussion started by: 793589
5 Replies

5. Shell Programming and Scripting

combine two perl lines into a single perl command

Hi Everyone, i have a string 00:44:40 so: $tmp=~ s/://gi; $tmp=~s/({2})({2})({2})/$1*3600+$2*60+$3/e; the output is 2680. Any way to combine this two lines into a single line? Thanks (4 Replies)
Discussion started by: jimmy_y
4 Replies

6. Shell Programming and Scripting

Is there a way to make this more efficient

I have the following code. printf "Test Message Report" > report.txt while read line do msgid=$(printf "%n" "$line" | cut -c1-6000| sed -e 's///g' -e 's|.*ex:Msg\(.*\)ex:Msg.*|\1|') putdate=$(printf "%n" "$line" | cut -c1-6000| sed -e 's///g' -e 's|.*PutDate\(.*\)PutTime.*|\1|')... (9 Replies)
Discussion started by: gugs
9 Replies

7. Shell Programming and Scripting

help on most efficient search

Hello, We have a directory with 15 sub-directories where each sub-directory contains 1.5 to 2 lakhs of files in it. Daily, around 300-500 files will be uploaded to each sub-directory. Now, i need to get the list of files received today in most efficient way. I tried using "find with newer... (16 Replies)
Discussion started by: prvnrk
16 Replies

8. Shell Programming and Scripting

Is there a more efficient way?

I'm using korn shell to connect to oracle, retrieve certain values, put them in a list, and iterate through them. While this method works, I can't help but think there is an easier method. If you know of one, please suggest a shorter, more efficient method. ############### FUNCTIONS ... (6 Replies)
Discussion started by: SelectSplat
6 Replies

9. Shell Programming and Scripting

How to combine "find" command in for each loop (tcsh)

Hello I was wandering if I can combine find command in side for each loop in unix the main propose is to change some thing in files from several types and not all of them is this possible ? (on liner script? ) tnx for the helppers (3 Replies)
Discussion started by: umen
3 Replies

10. UNIX for Dummies Questions & Answers

combine 2 lines (command & echo)

does anyone know how to combine 2 lines? this is what im playing around with. (filename: online, user name: prml0001, real name: primal) #!/bin/sh who | grep $1 > /dev/null if then grep $1 /etc/passwd | cut -f 5, -d : echo is logged on exit 0 else grep $1... (13 Replies)
Discussion started by: primal
13 Replies
Login or Register to Ask a Question