LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 03-16-2012, 10:01 AM   #1
saeedamer
LQ Newbie
 
Registered: Feb 2011
Posts: 25

Rep: Reputation: 0
help with awk-print please


Hi there,

I need help with retrieving particular value from the output of "ps -ef" command. Here is the situation:


Code:
 ps -eaf | grep "ora_pmon_" | grep -v 'grep'

  oracle 22020342        1   0   Mar 06      -  1:11 ora_pmon_db1 
  oracle 51577060        1   0 15:49:25      -  0:05 ora_pmon_db2
  oracle 52035836        1   0   Feb 28      -  2:37 ora_pmon_db3
  oracle 66125912        1   0   Feb 28      -  1:43 ora_pmon_db4
  oracle   459068        1   0 09:52:55      -  0:00 ora_pmon_db5
  oracle  3736018        1   0   Feb 28      -  2:08 ora_pmon_db6
Out of this output, I only want the substring of values in the last column (i.e., "db1, db2, db3..."). So this is what I tried:

Code:
ps -eaf | grep "ora_pmon_" | grep -v 'grep'|awk -F' ' '{print $9}' | nawk '{print substr($1,10)}'

OUTPUT:
=======
db1 

db3
db4

db6
Please note that "db2 and db5" did not show-up in the output. Now if I change my command to the following [{print $8} instead of {print $9}], I get "db2 and db5" but all others go missing.

Code:
ps -eaf | grep "ora_pmon_" | grep -v 'grep'|awk -F' ' '{print $8}' | nawk '{print substr($1,10)}'

OUTPUT:
=======
 
db2


db5

-- end of output
I want to get all values like this:
Code:
db1
db2
db3
db4
db5
db6
Could someone please help me how this can be done?

Thank so much in advance!!
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-16-2012, 11:00 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
you can't use fields normally as there are a different number of whitespace elements, just cut the string at the right character using substr() instead.
 
1 members found this post helpful.
Old 03-16-2012, 11:02 AM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
A brute-force approach...

Take the intermediate result you already have and do this ...
Code:
|rev          \
|cut -d_ -f1  \
|rev
... to keep whatever follows the right-most underscore.

Daniel B. Martin
 
1 members found this post helpful.
Old 03-16-2012, 12:26 PM   #4
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

The same as above using sed
Code:
| sed 's/.*_//'
 
2 members found this post helpful.
Old 03-16-2012, 12:28 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Simpler process in awk:
Code:
awk -F"_" '{print $NF}'
 
2 members found this post helpful.
Old 03-16-2012, 12:40 PM   #6
saeedamer
LQ Newbie
 
Registered: Feb 2011
Posts: 25

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by danielbmartin View Post
A brute-force approach...

Take the intermediate result you already have and do this ...
Code:
|rev          \
|cut -d_ -f1  \
|rev
... to keep whatever follows the right-most underscore.

Daniel B. Martin

It worked - thank you much Daniel:
Code:
ps -eaf | grep "ora_pmon_" | grep -v 'grep'|rev |cut -d_ -f1 |rev
I also found this but not sure if this is as good as your suggested solution:

Code:
 ps -ef |grep 'ora_pmon_'|grep -v grep| awk '/ora_pmon/ {print $NF}'
Thanks again!
 
Old 03-16-2012, 12:44 PM   #7
saeedamer
LQ Newbie
 
Registered: Feb 2011
Posts: 25

Original Poster
Rep: Reputation: 0
Thanks to Chris, grail and firstfire as well!!

Last edited by saeedamer; 03-16-2012 at 12:50 PM.
 
  


Reply



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
print certain digit only by AWK mauludi Linux - Newbie 1 08-19-2011 04:02 AM
print variable value in awk csegau Programming 3 05-16-2011 10:19 AM
[SOLVED] awk print only lines {not}containing FS grail Programming 8 04-12-2010 04:41 AM
Passing $i from for into awk print mmcc0912 Linux - General 2 04-06-2010 04:23 PM
How do I - not print - in awk? ZimMonkey Programming 10 08-21-2009 07:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:56 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration