LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-05-2010, 07:47 AM   #1
naveensankineni
LQ Newbie
 
Registered: Feb 2008
Posts: 11

Rep: Reputation: 0
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.
 
Old 08-05-2010, 08:49 AM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,028
Blog Entries: 5

Rep: Reputation: 790Reputation: 790Reputation: 790Reputation: 790Reputation: 790Reputation: 790Reputation: 790
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.
 
Old 08-05-2010, 08:58 AM   #3
zirias
Member
 
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59
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
 
Old 08-05-2010, 10:05 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,655

Rep: Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967
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)}'
 
1 members found this post helpful.
Old 08-05-2010, 11:26 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
ps -eo args |grep "/bin/tnslsn[r]" |sed 's|\/bin\/tnslsnr$||'
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
awk - gensub first result webhope Programming 3 05-27-2010 06:52 AM
cut part of a string using awk m4rtin Programming 2 09-03-2009 08:32 PM
How to use command grep,cut,awk to cut a data from a file? hocheetiong Linux - Newbie 7 09-11-2008 08:16 PM
I just cut and paste the code, why the result is different? keiwu Programming 22 02-17-2005 01:16 AM
cut / awk command?? Sammy2ooo Linux - Newbie 1 05-27-2003 06:46 PM


All times are GMT -5. The time now is 04:57 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration