- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: grep colums
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2010 11:01 AM
07-22-2010 11:01 AM
Please help!
my grep sometime returned with 8 or 9 columns as below.
how to change my scripts so the return will be more consistent
my gold is to get "pmon_PO"
#ps -ef|grep pmon
oracle 27676 1 0 Jun 3 ? 14:08 ora_pmon_PO
RETURN=`ps -ef|grep [p]mon_PO|awk '{print $9}'`
if [ "ora_pmon_$DB" = "$RETURN" ]
then
exit 0
else
Thank for your help.
Tom,
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2010 11:25 AM
07-22-2010 11:25 AM
SolutionUse 'awk' to match and pluck the last field:
# RETURN=$(ps -ef|awk '/pmon_PO/ {print $NF}')
...
For that matter, since you are matching the basename of a process and just want the pid (if any), do:
# PID=$(UNIX95= ps -C ora_pmon_PO -opid=)
Note the whitespace after the 'UNIX95='. This arms the XPG4 behavior only for the duration of the command line.
Regards!
...JRF...
- Tags:
- UNIX95
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2010 01:45 PM
07-22-2010 01:45 PM
Re: grep colums
it works,
#ps -ef|awk ' /pmon_PO/ {print $NF}'
ora_pmon_OP
$NF}
but sometimes it returned with "$NF"
how can i get rid of it.
Thanks very much,
Tom
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2010 02:17 PM
07-22-2010 02:17 PM
Re: grep colums
> but sometimes it returned with "$NF"
how can i get rid of it.
Yes, indeed this can happen and I should have accounted for it in my original post. Mea culpa.
Exercise this several times and occasionally you will see the extra line representing the 'awk' process we launched:
# ps -ef|awk '/cron/ {print $0,$NF}'
root 1555 1 0 Mar 3 ? 0:33 /usr/sbin/cron /usr/sbin/cron
root 20538 11785 1 18:07:41 pts/0 0:00 awk /cron/ {print $0,$NF} $0,$NF}
We don't want the last field of the second line!
We can do:
# ps -ef|awk '/cron/ && !/awk/ {print $NF}'
...which is the same rationale you see for command like:
# ps -ef|grep cron|grep -v cron
This all underscores, the value of the 'UNIX95' behavior I suggested where you exactly match a process basename ('-C
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2010 02:32 PM
07-22-2010 02:32 PM
Re: grep colums
thanks for your help,
i add the "grep at the end and it seems good enough for me.
Thanks again,
Tom
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2010 02:48 PM
07-22-2010 02:48 PM
Re: grep colums
> i add the "grep at the end and it seems good enough for me.
Don't mix 'grep' and 'awk' in a pipelined command. DOing so adds and extra process that most often can be subjugated into 'awk':
Look again at:
# ps -ef|awk '/cron/ && !~/awk/ {print $NF}'
This looks for a match to 'cron' only if there isn't the pattern 'awk' in the same line. Using the original snippet I offered, we amend it to be:
# RETURN=$(ps -ef|awk '/pmon_PO/ && !~/awk/ {print $NF}')
...no 'grep' needed and a process and some I/O saved. Thing green :-)
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2010 03:18 PM
07-22-2010 03:18 PM
Re: grep colums
Very dangerous. Never mix grep and ps! It isn't necessary and when you do, you get nasty results:
ps -ef | grep sh
But I was looking for the sh processes and got bash and ksh and unhashdaemon and even sherri, my receptionist!
Seriously, grep cannot be made to stay inside a field (column is so punch-cardy). While awk can extract the last field, what if the process you want is part of the command line, like grep ora_pmon_PO?
This construct will give you an EXACT match for all processes with the name ora_pmon_PO:
UNIX95=1 ps -f -C ora_pmon_PO
also:
UNIX95=1 ps -f -C sh
Notice: no grep. NOTE: UNIX95=1 is required to enable additional options such as -C, -H and -o. The -o option is really useful to customize your ps command:
UNIX95=1 ps -C ora_pmon_PO -o pid,vsz,args
The man page also tells you that you never grep for pid -- just use the -p option. Check out -H too.
Help stamp out ps|grep in HP-UX...
Bill Hassell, sysadmin