LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Need to cut the string in the result generated by AWK (https://www.linuxquestions.org/questions/programming-9/need-to-cut-the-string-in-the-result-generated-by-awk-824340/)

naveensankineni 08-05-2010 06:47 AM

Need to cut the string in the result generated by AWK
 
HI ,

I wanted to get the ORACLE_HOME of the listener's running on the server

I have been using the following to get the reult

/opt/oracle/admin/PHYPE/bdump> ps -ef|grep /bin/tnslsnr|grep -v grep|awk '{print $9}'

Result is as follows

/opt/oracle/product/10.2/bin/tnslsnr
/opt/oracle/product/9.2.0/bin/tnslsnr
/opt/oracle/product/10.2/bin/tnslsnr
/opt/oracle/product/10.2/bin/tnslsnr
/opt/oracle/product/9.2.0/bin/tnslsnr
/opt/oracle/product/10.2/bin/tnslsnr

Now i wanted to cut the /bin/tnslsnr part and the remaining should write to the file

Anyone please help me !!!

Thanks,
Naveen.

MensaWater 08-05-2010 07:49 AM

If there are always 4 components to PATH like that:
then piping what you have into
awk -F/ '{print "/"$2"/"$3"/"$4"/"$5}'
would work.

The "-F/" changes the field separator from space to "/". In the print side you have to put the slashes back in. Alternatively you could play with setting the OFS variable for the output field separator if the input had variable length paths.

zirias 08-05-2010 07:58 AM

Many shells (bash, zsh) support cutting ... do something like this:

Code:

for i in `ps ax --format cmd | grep "[/]bin/tnslsnr" | sort | uniq`; do
  echo ${i%/bin/tnslsnr}
done


grail 08-05-2010 09:05 AM

Geez ... how about we do away the slew of unnecessary greps and just let awk do its thing:
Code:

ps -ef | awk '"/bin/tnslsnr" && !/awk/{print gensub("/bin/tnslsnr","","g",$9)}'

ghostdog74 08-05-2010 10:26 AM

Code:

ps -eo args |grep "/bin/tnslsn[r]" |sed 's|\/bin\/tnslsnr$||'


All times are GMT -5. The time now is 09:18 AM.