Combine multiple awk commands

Hi Team,

I am getting input like below

$ ps -ef | grep pmon | grep -v asm | grep -v grep
oracle    3246     1  0 00:03 ?        00:00:01 ora_pmon_racora1
oracle    4367     1  0 00:03 ?        00:00:01 ora_pmon_test1
oracle    6893     1  0 00:03 ?        00:00:01 ora_pmon_gipora1
oracle    2156     1  0 00:03 ?        00:00:01 ora_pmon_vitest1

I want the output like below. Currently i can acheive via two awk commands but is there any way can do by one command.

$ ps -ef | grep pmon | grep -v grep | grep -v asm | awk '{print $1}'
$ ps -ef | grep pmon | grep -v grep | grep -v asm | awk -F_ '{print $NF}'
expected output :
-----------------

oracle racora1
oracle test1
oracle gipora1
oracle vitest1

Pls advice

Regards
Kamal

Try:

ps -ef | grep pmon | grep -v grep | grep -v asm | awk '
{	n = split($NF, u, /_/)
	print $1, u[n]
}'
1 Like

thanks perfectly working !

Try :

$ ps -ef | grep pmon | grep -v grep | grep -v asm | awk -F'[ _]' '{print $1,$NF}'
$ ps -ef | grep pmon | grep -v grep | grep -v asm | awk '{s=$1;sub(/.*_/,x);print s,$1}'
2 Likes

Excellent idea Akshay. Note, however, that some versions of awk sometimes (depending on field widths) use a tab as well as spaces as field delimiters. To be safe you might want to use:

awk -F'[ \t_]'

instead of:

awk -F'[ _]'
2 Likes

If want to avoid the some of the pipes,

ps -ef | awk '/pmon/ && !/asm/ && !/grep/ {x=$1;gsub(/.*_/,_);print x FS $0}'

I guess, the process owner would be always oracle ? as its an oracle process, may be you can fix that value if required (and acceptable).

ps -ef | awk '/pmon/ && !/asm/ && !/grep/ {gsub(/.*_/,_);print "oracle " $0}' 

Regex's greedy match removes everything until the last "_".

Another version could be :

ps -ef | awk '/pmon/ && !/asm|grep/ {gsub(/.*_/,_);print "oracle " $0}' 
1 Like

Thanks all for the valuable reply !!!