nsinha
1
Hi
I am writing a shell script in ksh where I have to grep for a process name , say XYZ from "ps -ef" and then extract it's PID.
ps -ef | grep XYZ gives -
" int 7738 25734 1 02:00:49 pts/tc 0:00 grep XYZ"
I am thinking of replacing one or more occurrences of the space with pipe "|", so the output will be -
|int|7738|25734|1|02:00:49|pts/tc|0:00|grep|XYZ
Then I want to use the "cut -d"|" -f3 which will give 7738, the PID.
But I am not able to replace one or more occurrences of the space with a single pipe. I have tried in many many different ways, but I am in vain.
Can you please show me the way how it can be done ?
Thanking you in advance ...
Nirmalya
do this:
ps -ef | grep XYZ | awk '{ print $2 }'
replace XYZ withwhatever you have
nsinha
3
Hi Yogesh
Thanks a lot man. I should also try 'awk' - my mistake.
Thanks
Nirmalya
Deepa
4
tr command should help you to squeeze characters.
Try tr -s option.
aigles
5
To replace spaces with pipe :
ps -ef | grep XYZ | sed 's/ */|/g'
I suggest you to use awk solution posted by Yogesh Sawant.
Your command will always returns the grep process, to avoid that you can use the following grep syntax :
ps -ef | grep [X]YZ
Jean-Pierre.
Am not sure, how would suppressing characters and that too which are repeated would prove to be helpful? Could you please explain that
how about this,
ps -ef | awk ' /XYZ/ { print $2 }'
better, that reduces one pipe