Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 08-05-2010, 06:47 AM   #1
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


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

Anyone please help me !!!

Old 08-05-2010, 07:49 AM   #2
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,986
Blog Entries: 14

Rep: Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187Reputation: 1187
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, 07:58 AM   #3
Registered: Jun 2010
Posts: 361

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

for i in `ps ax --format cmd | grep "[/]bin/tnslsnr" | sort | uniq`; do
  echo ${i%/bin/tnslsnr}
Old 08-05-2010, 09:05 AM   #4
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,544

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Geez ... how about we do away the slew of unnecessary greps and just let awk do its thing:
ps -ef | awk '"/bin/tnslsnr" && !/awk/{print gensub("/bin/tnslsnr","","g",$9)}'
1 members found this post helpful.
Old 08-05-2010, 10:26 AM   #5
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

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


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 05:52 AM
cut part of a string using awk m4rtin Programming 2 09-03-2009 07:32 PM
How to use command grep,cut,awk to cut a data from a file? hocheetiong Linux - Newbie 7 09-11-2008 07:16 PM
I just cut and paste the code, why the result is different? keiwu Programming 22 02-17-2005 12:16 AM
cut / awk command?? Sammy2ooo Linux - Newbie 1 05-27-2003 05:46 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:14 AM.

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