Need distinct values from command in a script

Hello,

I am using below command

srvctl config service -d cmdbut

[dev-oragrid-ux01]cmdbut_01 (P):/devoragridcn_01/app/oracle> srvctl config service -d cmdbut
Service name: boms10.world
Service is enabled
Server pool: cmdbut_boms10.world
Cardinality: 1
Disconnect: false
Service role: PRIMARY
Management policy: AUTOMATIC
DTP transaction: false
AQ HA notifications: false
Failover type: NONE
Failover method: NONE
TAF failover retries: 0
TAF failover delay: 0
Connection Load Balancing Goal: LONG
Runtime Load Balancing Goal: NONE
TAF policy specification: NONE
Edition:
Preferred instances: cmdbut_01
Available instances: cmdbut_02
Service name: boms_cmdbut_01.world
Service is enabled
Server pool: cmdbut_boms_cmdbut_01.world
Cardinality: 1
Disconnect: false
Service role: PRIMARY
Management policy: AUTOMATIC
DTP transaction: false
AQ HA notifications: false
Failover type: NONE
Failover method: NONE
TAF failover retries: 0
TAF failover delay: 0
Connection Load Balancing Goal: LONG
Runtime Load Balancing Goal: NONE
TAF policy specification: NONE
Edition:
Preferred instances: cmdbut_01
Available instances: cmdbut_02

This is supposed to give values as shown in the output and I need distinct values for Preferred instances: and Available instances: which means that all distinct values of Preferred instances and all distinct values of Available instances .Here ,Preferred instances: distinct value is cmdbut_01 and Available instances is cmdbut_02

Please help me in writing a script on this.
Best regards,
Vishal

Pipe command output to this awk program:

srvctl config service -d cmdbut | awk -F: '
        /Preferred instances/ {
                P[$NF]
        }
        /Available instances/ {
                A[$NF]
        }
        END {
                printf ( "%s:", "Preferred instances" )
                for ( k in P )
                        printf ( "%s ", k )

                printf ( "\n%s:", "Available instances" )
                for ( k in A )
                        printf ( "%s ", k )

                printf "\n"
        }
'
1 Like

Thank you!

Could you please give a little explanatory note with this so that I can understand the logic in the code

Best regards,
Vishal

awk -F: ' # Set colon (:) as field separator
          # Checking if pattern: Preferred instances exists in current record
          /Preferred instances/ {
                        # Add last field value ($NF) to index of associative array: P
                        P[$NF]
          }
          # Checking if pattern: Available instances exists in current record
          /Available instances/ {
                        # Add last field value ($NF) to index of associative array: A
                        A[$NF]
          }
          # END Rule
          END {
                        # Print Preferred instances
                        printf ( "%s:", "Preferred instances" )
                        # For each key in array: P
                        for ( k in P )
                                # Print key (k)
                                printf ( "%s ", k )

                        # Print Available instances
                        printf ( "\n%s:", "Available instances" )
                        # For each key in array: A
                        for ( k in A )
                                # Print key (k)
                                printf ( "%s ", k )

                        printf "\n"
        }
'
2 Likes

For older versions we have the output like below:

 srvctl config service -d p3fi_dev
p3fi_p3fi_dev.world PREF: p3fi_dev_01 AVAIL: p3fi_dev_02
pplnet_p3fidev PREF: p3fi_dev_01 AVAIL: p3fi_dev_02
nexus_p3fidev PREF: p3fi_dev_01 AVAIL: p3fi_dev_02
applog_p3fidev PREF: p3fi_dev_01 AVAIL: p3fi_dev_02
p3fi_dev_01 PREF: p3fi_dev_01 AVAIL: p3fi_dev_02

Can we put an option there that if it doesn't find Preferred instance it should look for PREF: keyword and AVAIL keyword

Best regards,
Vishal

Try this adjustment to Yoda's code to support both formats:

awk '
        /PREF:/ && /AVAIL:/ {
           for(i=1;i<NF;i++) {
              if($i == "PREF:") P[$(i+1)];
              if($i == "AVAIL:") A[$(i+1)];
           }
        }
        /Preferred instances/ {
                P[$NF]
        }
        /Available instances/ {
                A[$NF]
        }
        END {
                printf ( "%s:", "Preferred instances" )
                for ( k in P )
                        printf ( " %s", k )

                printf ( "\n%s:", "Available instances" )
                for ( k in A )
                        printf ( " %s", k )

                printf "\n"
        }
'

Hi ,

Preferred instances is still coming as empty.How to display PREFERRED now by matching PREF pattern and also I want to put this whole string to a variable which would contain both preferred and available instances and later on want to segregate preferred and available from that variable and assign that to 2 variables one for preferred and other for available

Best regards,
Vishal

Hi,Belwo is the output for p3fi_dev services

1/app/oracle> . ./oraprofile_p3fi_dev
[dev-oragrid-ux01]p3fi_dev_01 (P):/devoragridcn_01/app/oracle> srvctl config service -d p3fi_dev
p3fi_p3fi_dev.world PREF: p3fi_dev_01 AVAIL: p3fi_dev_02
pplnet_p3fidev PREF: p3fi_dev_01 AVAIL: p3fi_dev_02
nexus_p3fidev PREF: p3fi_dev_01 AVAIL: p3fi_dev_02
applog_p3fidev PREF: p3fi_dev_01 AVAIL: p3fi_dev_02

I am using below code

case $ORACLE_HOME in
  */product/10*)
# i# *)              echo "something else" ;;
#esac
Instance_value=`srvctl config service -d $database | awk -F: '
/PREF/ {
                P[$NF]
        }
        /AVAIL/ {
                A[$NF]
        }
        END {
                printf ( "%s:", "Preferred instances" )
                for ( k in P )
                        printf ( "%s ", k )

                printf ( "\n%s:", "Available instances" )
                for ( k in A )
                        printf ( "%s ", k )

                printf "\n"
        }
'`;;

however this is giving me blank values for preferred and available instance

Please help on this

Best regards,
Vishal