Combine Multiple AWK Commands

Problem 1:

I am getting input like below:

Code:

$ 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 achieve via two awk commands but is there any way can do by one command.

Code:

$ 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}'

Code:

expected output :
-----------------
oracle racora1
oracle test1
oracle gipora1
oracle vitest1

Solution 1:

Try:

Code:

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

Or if want to avoid the some of the pipes,

Code:

ps -ef | awk '/pmon/ && !/asm/ && !/grep/ {x=$1;gsub(/.*_/,_);print x FS $0}'
I guess, the process owner would be always oracle as it’s an oracle process, may be you can fix that value if required (and acceptable).
 

Code:

ps -ef | awk '/pmon/ && !/asm/ && !/grep/ {gsub(/.*_/,_);print "oracle " $0}'
Regex's greedy match removes everything until the last "_".

Another version could be :

Code:

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

---

Problem 2:

I have a scenario where in, I have a file named abc.txt.
I extract the file names from it using.

Code:

awk '/dbf$/{print $NF}' abc.txt
/u01/oradata/omc/systab/omcdef.dbf
/u01/oradata/omc/oratemp/temptab1.dbf

Now I need to further enhance this command and also extract the mount point details using df -k
E.g. The final output should be like

Code:

The mount point details for '/u01/oradata/omc/systab/omcdef.dbf' are:

/u01/oradata/omc/systab (/dev/vx/dsk/omcdg/systab_v) : 15080098 total allocated Kb
9728233 free allocated Kb
5351865 used allocated Kb
35 % allocation used

The mount point details for '/u01/oradata/omc/oratemp/temptab1.dbf' are:

/u01/oradata/omc/oratemp (/dev/vx/dsk/omcdg/oratemp_v) : 11305721 total allocated Kb
3429324 free allocated Kb
7876397 used allocated Kb
69 % allocation used
 

How do I achieve this with AWK?

Solution 2:

Try to use Following :

Code:

for i in `awk '/dbf$/{print $NF}' abc.txt`
do
Dev=`df -k $i|sed 1d|awk '{print $6}'`
Tot=`df -k $i|sed 1d|awk '{print $2}'`
Free=`df -k $i|sed 1d|awk '{print $4}'`
Used=`df -k $i|sed 1d|awk '{print $3}'`
Per=`df -k $i|sed 1d|awk '{print $5}'`
echo "The mount point details for $i are :"
echo " $i      ( $Dev )       :      $Tot  total allocated kb "
echo " $Free free allocated Kb "
echo "$Used  used allocated Kb"
echo "$Per % allocation used"
done

Or using an array to minimize the calls to awk :

Code:

#!/bin/bash
for F in $(awk '/dbf$/{print $NF}' abc.txt)
do
    A=( $(df -k $F | sed 1d) )
    echo "The mount point details for $F are :"
    echo "$F      ( ${A[5]} )       :      ${A[1]}  total allocated kb"
    echo "${A[3]} free allocated Kb"
    echo "${A[2]} used allocated Kb"
    echo "${A[4]}% allocation used"
done

Unix Tips

See Also
Review The History And The Commands

Have a Unix Problem
Do you have a UNIX Question?

Unix Books :-
UNIX Programming, Certification, System Administration, Performance Tuning Reference Books

Return to : - Unix System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.